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