我正在使用JPA类创建数据库.
如果我们有ManyToOne关系,我们可以覆盖ForeignKey名称,如下所示:
@ManyToOne @JoinColumn(foreignKey = @ForeignKey(name = "FK_COUNTRY")) private Country country;
在DB中,我们将得到如下结果:
好的,那很好好的结果!
但是当我有@ManyToMany时,我不会设置自己的FK名称.
如何创建这个?我尝试这样的东西,但它不起作用:
@ManyToMany(cascade = CascadeType.PERSIST,fetch = FetchType.LAZY) @JoinTable(name = "NEW_TABLE",foreignKey = @ForeignKey(name = "FK_TEST"))
或者这样的东西:
@JoinTable( name="NEW_TABLE",joinColumns= @JoinColumn(name="ID1",referencedColumnName="ID",foreignKey = @ForeignKey(name = "FK_DEV_ID")),inverseJoinColumns=ID2",foreignKey = @ForeignKey(name = "FK_DEV_ZONE")) )
或这个:
@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;
他们不工作
我使用这个版本的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>
而我的java版本是1.8