Multiple instances of the same embeddable class can be embedded in the same entity. In that case we cannot use the default column names of the embeddable fields/properties; We must resolve the conflicts between the same attributes of the different instances. Although, we can use @Column annotation in the embeddable class, but in this specific case where we have multiple instances of the same embeddable, we must have a way to specify different column names for different 'instances'. For that we have to use @AttributeOverrides and @AttributeOverride annotations. Let's understand that with an example.
Example
@Embeddable
public class ClassA {
private String myStr;
private int myInt;
.............
}
@Entity
public class EntityA {
@Id
@GeneratedValue
private int id;
@AttributeOverrides({
@AttributeOverride(name = "myStr", column = @Column(name = "MY_STR_COL1")),
@AttributeOverride(name = "myInt", column = @Column(name = "MY_INT_COL1"))
})
@Embedded
private ClassA classARef;
@AttributeOverrides({
@AttributeOverride(name = "myStr", column = @Column(name = "MY_STR_COL2")),
@AttributeOverride(name = "myInt", column = @Column(name = "MY_INT_COL2"))
})
@Embedded
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");
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] 'SHOW COLUMNS from EntityA' [ID, INTEGER(10), NO, PRI, NULL] [MY_INT_COL1, INTEGER(10), YES, , NULL] [MY_STR_COL1, VARCHAR(255), YES, , NULL] [MY_INT_COL2, INTEGER(10), YES, , NULL] [MY_STR_COL2, 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();
EntityA entityA = new EntityA();
ClassA a = new ClassA();
a.setMyInt(10);
a.setMyStr("a test String");
entityA.setClassARef(a);
ClassA a2 = new ClassA();
a2.setMyInt(20);
a2.setMyStr("a test String 2");
entityA.setClassARef2(a2);
System.out.println(entityA);
em.getTransaction().begin();
em.persist(entityA);
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");
}
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);
em.close();
}
} Output-- Persisting entities -- EntityA{id=0, classARef=ClassA{myStr='a test String', myInt=10}, classARef2=ClassA{myStr='a test String 2', myInt=20}} -- native queries -- 'Select * from EntityA' [1, 10, a test String, 20, a test String 2] -- Loading EntityA -- EntityA{id=1, classARef=ClassA{myStr='a test String', myInt=10}, classARef2=ClassA{myStr='a test String 2', myInt=20}}
org.hibernate.MappingException: Repeated column in mapping
If we don't use @AttributeOverrides then we will have MappingException. Let's check that out:
@Entity
public class EntityA {
@Id
@GeneratedValue
private int id;
@Embedded
private ClassA classARef;
@Embedded
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: myInt (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)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:849)
........................
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
|
|