I have generic class that extend other generic class. All of them is @MappedSuperclass
. So they dont have own table in database. They are also abstract, so they dont have any objects. They are just skeleton for my entities @Entity
My inheritance structure:
Generic -> GenericDictionary -> GenericHierarchicalDictionary -> Unit
Where Unit is @Entity
class and have objects.
@Entity
public class Unit extends GenericHierarchicalDictionary<Unit> {}
Unit entity has hierarchical structure which means that Entity has relation to itself, but I am using abstract class (@MappedSuperclass
) for that, so I would like define it dynamically in that abstract class:
My GenericHierarchicalDictionary:
@MappedSuperclass
public abstract class GenericHierarchicalDictionary<T extends Generic<T>> extends GenericDictionary<T> {
@Required
@ManyToOne(targetEntity = GenericHierarchicalDictionary.class, fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinColumn(name="parent_id")
public GenericHierarchicalDictionary<T> parent;
But id dosnt work. I am getting an error:
play.api.UnexpectedException: Unexpected exception[PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to configure EntityManagerFactory]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:148) ~[play_2.10.jar:2.2.4]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.4]
at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.4]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:110) ~[play_2.10.jar:2.2.4]
at scala.util.Success.flatMap(Try.scala:200) ~[scala-library.jar:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to configure EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:378) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35) ~[play-java-jpa_2.10.jar:2.2.4]
at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:88) ~[play_2.10.jar:2.2.4]
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on models.Unit.parent references an unknown entity: models.GenericHierarchicalDictionary
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:107) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1580) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1503) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1419) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1519) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
If I get right that error, I should assign to targetEntity a java class with @Entity
annotation, but it is not what I would like to have.
I would like to have @ManyToOne targetEntity declared dynamically from context of Entity.
How can I achive this? Please give me some help.
Here is my hierarchical generic class (abstract, mapped superclass) implemented in spring:
Do all job. I have working implementation base on it class. Hope I will help.
Check this post: Java hibernate/jpa how to create dynamic generic entity that is self related