HHH-10385 HHH-10386 - Add tests for issues

This commit is contained in:
Andrea Boriero 2015-12-21 13:39:16 +00:00
parent 5b42faa134
commit 504dd8353d
5 changed files with 165 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Project> projects;
}

View File

@ -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 );

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Phone> phones = new ArrayList<>();
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Employee> employees;
}