diff --git a/hibernate-core/src/test/java/org/hibernate/test/inheritancediscriminator/embeddable/JoinedSubclassWithEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/test/inheritancediscriminator/embeddable/JoinedSubclassWithEmbeddableTest.java new file mode 100644 index 0000000000..a68cbaad91 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/inheritancediscriminator/embeddable/JoinedSubclassWithEmbeddableTest.java @@ -0,0 +1,160 @@ +/* + * 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.inheritancediscriminator.embeddable; + +import javax.persistence.CollectionTable; +import javax.persistence.Embeddable; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; +import javax.persistence.MapKeyColumn; +import javax.persistence.OneToMany; +import javax.persistence.OrderColumn; +import javax.persistence.Table; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; + +/** + * @author Andrea Boriero + */ +@TestForIssue(jiraKey = "HHH-10037") +public class JoinedSubclassWithEmbeddableTest extends BaseCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {Person.class, Employee.class}; + } + + @Test + public void testSelectFromEmbeddedField() { + doInHibernate( this::sessionFactory, session -> { + session.createNativeQuery( "select * from employee_embeddable_person_map" ).getResultList(); + } ); + } + + @Test + public void testSelectFromSubclass() { + doInHibernate( this::sessionFactory, session -> { + session.createNativeQuery( "select * from embeddable_person_map" ).getResultList(); + } ); + } + + @Test + public void testSelectFromParent() { + doInHibernate( this::sessionFactory, session -> { + session.createNativeQuery( "select * from person_map" ).getResultList(); + } ); + } + + @Entity + @Inheritance(strategy = InheritanceType.JOINED) + public static abstract class Person implements Serializable { + + @Id + @GeneratedValue + private Long id; + private String name; + + @Embedded + private Contact contact; + + @ManyToOne + @JoinColumn(name = "alert_contact") + private Person alertContact; + + @OneToMany + @JoinColumn(name = "alert_contact") + private Set alerteeContacts = new HashSet<>(); + + @ManyToMany + @OrderColumn(name = "list_idx") + @JoinTable(name = "person_list") + private List personList = new ArrayList<>(); + + @ManyToMany + @CollectionTable(name = "person_map") + @MapKeyColumn(name = "person_key", length = 20) + private Map personMap = new HashMap<>(); + + public Person() { + } + + public Person(String name) { + this.name = name; + } + } + + @Entity + @Table(name = "employees") + public static class Employee extends Person { + private Integer employeeNumber; + + @Embedded + private EmployeeContact employeeContact; + } + + @Embeddable + public static class Contact implements Serializable { + @ManyToOne + @JoinColumn(name = "embeddable_alert_contact") + private Person alertContact; + + @OneToMany + @JoinColumn(name = "embeddable_alert_contact") + private Set alerteeContacts = new HashSet<>(); + + @ManyToMany + @OrderColumn(name = "list_idx") + @JoinTable(name = "embeddable_person_list") + private List personList = new ArrayList<>(); + + @ManyToMany + @CollectionTable(name = "embeddable_person_map") + @MapKeyColumn(name = "person_key", length = 20) + private Map personMap = new HashMap<>(); + } + + @Embeddable + public class EmployeeContact implements Serializable { + + @ManyToOne + @JoinColumn(name = "employee_embeddable_alert_contact") + private Person alertContact; + + @OneToMany + @JoinColumn(name = "employee_embeddable_alert_contact") + private Set alerteeContacts = new HashSet<>(); + + @ManyToMany + @OrderColumn(name = "list_idx") + @JoinTable(name = "employee_embeddable_person_list") + private List personList = new ArrayList<>(); + + @ManyToMany + @CollectionTable(name = "employee_embeddable_person_map") + @MapKeyColumn(name = "person_key", length = 20) + private Map personMap = new HashMap<>(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/inheritancediscriminator/embeddable/SingleTableWithEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/test/inheritancediscriminator/embeddable/SingleTableWithEmbeddableTest.java new file mode 100644 index 0000000000..f77f185171 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/inheritancediscriminator/embeddable/SingleTableWithEmbeddableTest.java @@ -0,0 +1,161 @@ +/* + * 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.inheritancediscriminator.embeddable; + +import javax.persistence.CollectionTable; +import javax.persistence.Embeddable; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; +import javax.persistence.MapKeyColumn; +import javax.persistence.OneToMany; +import javax.persistence.OrderColumn; +import javax.persistence.Table; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; + +/** + * @author Andrea Boriero + */ +@TestForIssue(jiraKey = "HHH-10037") +public class SingleTableWithEmbeddableTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {Person.class, Employee.class}; + } + + @Test + public void testSelectFromEmbeddedField() { + doInHibernate( this::sessionFactory, session -> { + session.createNativeQuery( "select * from employee_embeddable_person_map" ).getResultList(); + } ); + } + + @Test + public void testSelectFromSubclass() { + doInHibernate( this::sessionFactory, session -> { + session.createNativeQuery( "select * from embeddable_person_map" ).getResultList(); + } ); + } + + @Test + public void testSelectFromParent() { + doInHibernate( this::sessionFactory, session -> { + session.createNativeQuery( "select * from person_map" ).getResultList(); + } ); + } + + @Entity + @Inheritance(strategy = InheritanceType.SINGLE_TABLE) + public static abstract class Person implements Serializable { + + @Id + @GeneratedValue + private Long id; + private String name; + + @Embedded + private Contact contact; + + @ManyToOne + @JoinColumn(name = "alert_contact") + private Person alertContact; + + @OneToMany + @JoinColumn(name = "alert_contact") + private Set alerteeContacts = new HashSet<>(); + + @ManyToMany + @OrderColumn(name = "list_idx") + @JoinTable(name = "person_list") + private List personList = new ArrayList<>(); + + @ManyToMany + @CollectionTable(name = "person_map") + @MapKeyColumn(name = "person_key", length = 20) + private Map personMap = new HashMap<>(); + + public Person() { + } + + public Person(String name) { + this.name = name; + } + } + + @Entity + @Table(name = "employees") + public static class Employee extends Person { + private Integer employeeNumber; + + @Embedded + private EmployeeContact employeeContact; + } + + @Embeddable + public static class Contact implements Serializable { + @ManyToOne + @JoinColumn(name = "embeddable_alert_contact") + private Person alertContact; + + @OneToMany + @JoinColumn(name = "embeddable_alert_contact") + private Set alerteeContacts = new HashSet<>(); + + @ManyToMany + @OrderColumn(name = "list_idx") + @JoinTable(name = "embeddable_person_list") + private List personList = new ArrayList<>(); + + @ManyToMany + @CollectionTable(name = "embeddable_person_map") + @MapKeyColumn(name = "person_key", length = 20) + private Map personMap = new HashMap<>(); + } + + @Embeddable + public class EmployeeContact implements Serializable { + + @ManyToOne + @JoinColumn(name = "employee_embeddable_alert_contact") + private Person alertContact; + + @OneToMany + @JoinColumn(name = "employee_embeddable_alert_contact") + private Set alerteeContacts = new HashSet<>(); + + @ManyToMany + @OrderColumn(name = "list_idx") + @JoinTable(name = "employee_embeddable_person_list") + private List personList = new ArrayList<>(); + + @ManyToMany + @CollectionTable(name = "employee_embeddable_person_map") + @MapKeyColumn(name = "person_key", length = 20) + private Map personMap = new HashMap<>(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/inheritancediscriminator/embeddable/TablePerClassWithEmbeddableTest.java b/hibernate-core/src/test/java/org/hibernate/test/inheritancediscriminator/embeddable/TablePerClassWithEmbeddableTest.java new file mode 100644 index 0000000000..3859a8fd30 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/inheritancediscriminator/embeddable/TablePerClassWithEmbeddableTest.java @@ -0,0 +1,160 @@ +/* + * 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.inheritancediscriminator.embeddable; + +import javax.persistence.CollectionTable; +import javax.persistence.Embeddable; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; +import javax.persistence.MapKeyColumn; +import javax.persistence.OneToMany; +import javax.persistence.OrderColumn; +import javax.persistence.Table; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; + +/** + * @author Andrea Boriero + */ +@TestForIssue(jiraKey = "HHH-10037") +public class TablePerClassWithEmbeddableTest extends BaseCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {Person.class, Employee.class}; + } + + @Test + public void testSelectFromEmbeddedField() { + doInHibernate( this::sessionFactory, session -> { + session.createNativeQuery( "select * from employee_embeddable_person_map" ).getResultList(); + } ); + } + + @Test + public void testSelectFromSubclass() { + doInHibernate( this::sessionFactory, session -> { + session.createNativeQuery( "select * from embeddable_person_map" ).getResultList(); + } ); + } + + @Test + public void testSelectFromParent() { + doInHibernate( this::sessionFactory, session -> { + session.createNativeQuery( "select * from person_map" ).getResultList(); + } ); + } + + @Entity + @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) + public static abstract class Person implements Serializable { + + @Id + @GeneratedValue + private Long id; + private String name; + + @Embedded + private Contact contact; + + @ManyToOne + @JoinColumn(name = "alert_contact") + private Person alertContact; + + @OneToMany + @JoinColumn(name = "alert_contact") + private Set alerteeContacts = new HashSet<>(); + + @ManyToMany + @OrderColumn(name = "list_idx") + @JoinTable(name = "person_list") + private List personList = new ArrayList<>(); + + @ManyToMany + @CollectionTable(name = "person_map") + @MapKeyColumn(name = "person_key", length = 20) + private Map personMap = new HashMap<>(); + + public Person() { + } + + public Person(String name) { + this.name = name; + } + } + + @Entity + @Table(name = "employees") + public static class Employee extends Person { + private Integer employeeNumber; + + @Embedded + private EmployeeContact employeeContact; + } + + @Embeddable + public static class Contact implements Serializable { + @ManyToOne + @JoinColumn(name = "embeddable_alert_contact") + private Person alertContact; + + @OneToMany + @JoinColumn(name = "embeddable_alert_contact") + private Set alerteeContacts = new HashSet<>(); + + @ManyToMany + @OrderColumn(name = "list_idx") + @JoinTable(name = "embeddable_person_list") + private List personList = new ArrayList<>(); + + @ManyToMany + @CollectionTable(name = "embeddable_person_map") + @MapKeyColumn(name = "person_key", length = 20) + private Map personMap = new HashMap<>(); + } + + @Embeddable + public class EmployeeContact implements Serializable { + + @ManyToOne + @JoinColumn(name = "employee_embeddable_alert_contact") + private Person alertContact; + + @OneToMany + @JoinColumn(name = "employee_embeddable_alert_contact") + private Set alerteeContacts = new HashSet<>(); + + @ManyToMany + @OrderColumn(name = "list_idx") + @JoinTable(name = "employee_embeddable_person_list") + private List personList = new ArrayList<>(); + + @ManyToMany + @CollectionTable(name = "employee_embeddable_person_map") + @MapKeyColumn(name = "person_key", length = 20) + private Map personMap = new HashMap<>(); + } +}