From 504dd8353db88258c2a12b8c3b607c76e885fdfc Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 21 Dec 2015 13:39:16 +0000 Subject: [PATCH] HHH-10385 HHH-10386 - Add tests for issues --- .../schemaupdate/foreignkeys/Employee.java | 32 +++++++++++++ .../foreignkeys/ForeignKeyGenerationTest.java | 48 +++++++++++++++++++ .../test/schemaupdate/foreignkeys/Person.java | 37 ++++++++++++++ .../test/schemaupdate/foreignkeys/Phone.java | 23 +++++++++ .../schemaupdate/foreignkeys/Project.java | 25 ++++++++++ 5 files changed, 165 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Employee.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Person.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Phone.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Project.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Employee.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Employee.java new file mode 100644 index 0000000000..e175963d93 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Employee.java @@ -0,0 +1,32 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.schemaupdate.foreignkeys; + +import javax.persistence.Entity; +import javax.persistence.ForeignKey; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.Table; +import java.util.Set; + +@Entity +@Table(name = "EMPLOYEE") +public class Employee { + @Id + @GeneratedValue + private Long id; + + @ManyToMany + @JoinTable(name = "EMPLOYEE_PROJECT", + joinColumns = @JoinColumn(name = "EMPLOYEE_ID", foreignKey = @ForeignKey(name = "FK_EMPLOYEE")), + inverseJoinColumns = @JoinColumn(name = "PROJECT_ID", foreignKey = @ForeignKey(name = "FK_PROJECT"))) + private Set projects; + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyGenerationTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyGenerationTest.java index 7dd1547ea8..6315828f60 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyGenerationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyGenerationTest.java @@ -89,6 +89,54 @@ public class ForeignKeyGenerationTest extends BaseUnitTestCase { ) ); } + @Test + @TestForIssue(jiraKey = "HHH-10385") + public void oneToManyWithJoinTableTest() throws Exception { + createSchema( new Class[] {Person.class, Phone.class} ); + + /* + The generated SQL for the foreign keys should be: + alter table PERSON_PHONE add constraint PERSON_ID_FK foreign key (PERSON_ID) references PERSON + alter table PERSON_PHONE add constraint PHONE_ID_FK foreign key (PHONE_ID) references PHONE + */ + checkAlterTableStatement( new AlterTableStatement( + "PERSON_PHONE", + "PERSON_ID_FK", + "PERSON_ID", + "PERSON" + ) ); + checkAlterTableStatement( new AlterTableStatement( + "PERSON_PHONE", + "PHONE_ID_FK", + "PHONE_ID", + "PHONE" + ) ); + } + + @Test + @TestForIssue(jiraKey = "HHH-10386") + public void manyToManyTest() throws Exception { + createSchema( new Class[] {Project.class, Employee.class} ); + + /* + The generated SQL for the foreign keys should be: + alter table EMPLOYEE_PROJECT add constraint FK_EMPLOYEE foreign key (EMPLOYEE_ID) references EMPLOYEE + alter table EMPLOYEE_PROJECT add constraint FK_PROJECT foreign key (PROJECT_ID) references PROJECT + */ + checkAlterTableStatement( new AlterTableStatement( + "EMPLOYEE_PROJECT", + "FK_EMPLOYEE", + "EMPLOYEE_ID", + "EMPLOYEE" + ) ); + checkAlterTableStatement( new AlterTableStatement( + "EMPLOYEE_PROJECT", + "FK_PROJECT", + "PROJECT_ID", + "PROJECT" + ) ); + } + private void createSchema(Class[] annotatedClasses) { final MetadataSources metadataSources = new MetadataSources( ssr ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Person.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Person.java new file mode 100644 index 0000000000..516870d9e2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Person.java @@ -0,0 +1,37 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.schemaupdate.foreignkeys; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.ForeignKey; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Andrea Boriero + */ +@Entity +@Table(name = "PERSON") +public class Person { + @Id + @GeneratedValue + private Long id; + + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) + @JoinTable(name = "PERSON_PHONE", + joinColumns = @JoinColumn(name = "PERSON_ID", foreignKey = @ForeignKey(name = "PERSON_ID_FK")), + inverseJoinColumns = @JoinColumn(name = "PHONE_ID", foreignKey = @ForeignKey(name = "PHONE_ID_FK")) + ) + private List phones = new ArrayList<>(); +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Phone.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Phone.java new file mode 100644 index 0000000000..71f1ba89c8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Phone.java @@ -0,0 +1,23 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.schemaupdate.foreignkeys; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author Andrea Boriero + */ +@Entity +@Table(name = "PHONE") +public class Phone { + @Id + private Long id; + + private String number; +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Project.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Project.java new file mode 100644 index 0000000000..53abe370c3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/Project.java @@ -0,0 +1,25 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.schemaupdate.foreignkeys; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.persistence.Table; +import java.util.Set; + +@Entity +@Table(name = "PROJECT") +public class Project { + @Id + @GeneratedValue + private Long id; + + @ManyToMany(mappedBy="projects") + private Set employees; +}