Multiple instances of the same embeddable class can be embedded in the same entity. We saw in this example how to resolve conflicting column names by using @AttributeOverrides and @AttributeOverride annotations. In case if the target embeddable class has an association with another entity then there will be join column name conflict as well; to resolve that conflict, we need to use @AssociationOverrides and @AssociationOverride annotations. Let's see an example how to do that.
Example
Following embeddable class has ManyToOne relationship with EntityB:
@Embeddable
public class ClassA {
private String myStr;
@ManyToOne
private EntityB entityBRef;
.............
}
@Entity
public class EntityB {
@Id
@GeneratedValue
private int myIdB;
private String myStr2;
.............
}
EntityA is using multiple instances of embeddable ClassA. This class needs to resolve all conflicting column names:
@Entity
public class EntityA {
@Id
@GeneratedValue
private int id;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "myStr", column = @Column(name = "MY_STR_COL1"))
})
@AssociationOverrides({
@AssociationOverride(
name = "entityBRef", joinColumns = @JoinColumn(name = "EntityAB_JOIN1"))
})
private ClassA classARef;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "myStr", column = @Column(name = "MY_STR_COL2"))
})
@AssociationOverrides({
@AssociationOverride(
name = "entityBRef", joinColumns = @JoinColumn(name = "EntityAB_JOIN2"))
})
private ClassA classARef2;
.............
}
public class ExampleMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("example-unit");
try {
EntityManager em = emf.createEntityManager();
nativeQuery(em, "SHOW TABLES");
nativeQuery(em, "SHOW COLUMNS from EntityA");
nativeQuery(em, "SHOW COLUMNS from EntityB");
emf.close();
} finally {
emf.close();
}
}
public static void nativeQuery(EntityManager em, String s) {
System.out.printf("'%s'%n", s);
Query query = em.createNativeQuery(s);
List list = query.getResultList();
for (Object o : list) {
if(o instanceof Object[]) {
System.out.println(Arrays.toString((Object[]) o));
}else{
System.out.println(o);
}
}
}
} Output'SHOW TABLES' [ENTITYA, PUBLIC] [ENTITYB, PUBLIC] 'SHOW COLUMNS from EntityA' [ID, INTEGER(10), NO, PRI, NULL] [MY_STR_COL1, VARCHAR(255), YES, , NULL] [MY_STR_COL2, VARCHAR(255), YES, , NULL] [ENTITYAB_JOIN1, INTEGER(10), YES, , NULL] [ENTITYAB_JOIN2, INTEGER(10), YES, , NULL] 'SHOW COLUMNS from EntityB' [MYIDB, INTEGER(10), NO, PRI, NULL] [MYSTR2, VARCHAR(255), YES, , NULL]
Persisting and loading data
public class ExampleMain2 {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("example-unit");
try {
persistEntity(emf);
runNativeQueries(emf);
loadEntity(emf);
} finally {
emf.close();
}
}
private static void persistEntity(EntityManagerFactory emf) {
System.out.println("-- Persisting entities --");
EntityManager em = emf.createEntityManager();
EntityB entityB = new EntityB();
entityB.setMyStr2("a test string in EntityB");
ClassA a = new ClassA();
a.setMyStr("a test string in ClassA");
a.setEntityBRef(entityB);
EntityB entityB2 = new EntityB();
entityB2.setMyStr2("a test string in EntityB 2");
ClassA a2 = new ClassA();
a2.setMyStr("a test string in ClassA 2");
a2.setEntityBRef(entityB2);
EntityA entityA = new EntityA();
entityA.setClassARef(a);
entityA.setClassARef2(a2);
System.out.println(entityA);
em.getTransaction().begin();
em.persist(entityA);
em.persist(entityB);
em.persist(entityB2);
em.getTransaction().commit();
em.close();
}
private static void runNativeQueries(EntityManagerFactory emf) {
System.out.println("-- native queries --");
EntityManager em = emf.createEntityManager();
ExampleMain.nativeQuery(em, "Select * from EntityA");
ExampleMain.nativeQuery(em, "Select * from EntityB");
}
private static void loadEntity(EntityManagerFactory emf) {
System.out.println("-- Loading EntityA --");
EntityManager em = emf.createEntityManager();
List<EntityA> entityAList = em.createQuery("Select t from EntityA t")
.getResultList();
entityAList.forEach(System.out::println);
System.out.println("-- Loading EntityB --");
List<EntityB> entityBList = em.createQuery("Select t from EntityB t")
.getResultList();
entityBList.forEach(System.out::println);
em.close();
}
} Output-- Persisting entities -- EntityA{id=0, classARef=ClassA{myStr='a test string in ClassA', entityBRef=EntityB{myIdB=0, myStr2='a test string in EntityB'}}, classARef2=ClassA{myStr='a test string in ClassA 2', entityBRef=EntityB{myIdB=0, myStr2='a test string in EntityB 2'}}} -- native queries -- 'Select * from EntityA' [1, a test string in ClassA, a test string in ClassA 2, 2, 3] 'Select * from EntityB' [2, a test string in EntityB] [3, a test string in EntityB 2] -- Loading EntityA -- EntityA{id=1, classARef=ClassA{myStr='a test string in ClassA', entityBRef=EntityB{myIdB=2, myStr2='a test string in EntityB'}}, classARef2=ClassA{myStr='a test string in ClassA 2', entityBRef=EntityB{myIdB=3, myStr2='a test string in EntityB 2'}}} -- Loading EntityB -- EntityB{myIdB=2, myStr2='a test string in EntityB'} EntityB{myIdB=3, myStr2='a test string in EntityB 2'}
org.hibernate.MappingException: Repeated column in mapping
If we don't use @AssociativeOverrides then we will have MappingException. Let's check that out:
@Entity
public class EntityA {
@Id
@GeneratedValue
private int id;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "myStr", column = @Column(name = "MY_STR_COL1"))
})
/* @AssociationOverrides({
@AssociationOverride(
name = "entityBRef", joinColumns = @JoinColumn(name = "EntityAB_JOIN1"))
})*/
private ClassA classARef;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "myStr", column = @Column(name = "MY_STR_COL2"))
})
/* @AssociationOverrides({
@AssociationOverride(
name = "entityBRef", joinColumns = @JoinColumn(name = "EntityAB_JOIN2"))
})*/
private ClassA classARef2;
.............
}
Output
On running ExampleMain, we will have following output
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.logicbig.example.EntityA column: entityBRef_myIdB (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:835)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:853) ........................
Example ProjectDependencies and Technologies Used: - h2 1.4.193: H2 Database Engine.
- hibernate-core 5.2.8.Final: The core O/RM functionality as provided by Hibernate.
Implements javax.persistence:javax.persistence-api version 2.1 - JDK 1.8
- Maven 3.3.9
|
|