Java – how to set the ForeignKey name when I have @ manytomany
•
Java
I am using JPA class to create database
If we have a manytoone relationship, we can override the ForeignKey name, as shown below:
@ManyToOne @JoinColumn(foreignKey = @ForeignKey(name = "FK_COUNTRY")) private Country country;
In dB, we will get the following results:
OK, that's a good result!
But when I have @ manytomany, I won't set my own FK name
How do I create this? I tried something like this, but it didn't work:
@ManyToMany(cascade = CascadeType.PERSIST,fetch = FetchType.LAZY) @JoinTable(name = "NEW_TABLE",foreignKey = @ForeignKey(name = "FK_TEST"))
Or something like this:
@JoinTable(
name="NEW_TABLE",joinColumns=
@JoinColumn(name="ID1",referencedColumnName="ID",foreignKey = @ForeignKey(name = "FK_DEV_ID")),inverseJoinColumns=ID2",foreignKey = @ForeignKey(name = "FK_DEV_ZONE"))
)
Or this:
@ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(name="NEW_TABLE_2",joinColumns=
@JoinColumn(name="ID1",foreignKey = @ForeignKey(name = "FK_1")
),inverseJoinColumns=
@JoinColumn(name="ID2",foreignKey = @ForeignKey(name = "FK_2")
),foreignKey = @ForeignKey(name = "FK_1"),inverseForeignKey = @ForeignKey(name = "FK_2")
)
private List<MyObject> deviceZones;
They don't work
I use this version of jar:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.2.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
My java version is 1.8
Solution
I guess you have some bottle conflicts
>Delete the following jars: Hibernate commons annotations and hibernate entitymanager > and see how you can change the name of foreign keys here
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
