Apply the common class @MappedSuperclass as targetEntity.Error: @ManyToOne on models.Unit.parent refers to an unknown object: models.GenericHierarchic

I have a generic class that extends another generic class. They are all @MappedSuperclass . Thus, they do not have their own table in the database. They are also abstract, so they have no objects. This is just a skeleton for my @Entity entities

My inheritance structure:

Generic -> GenericDictionary -> GenericHierarchicalDictionary -> Unit

Where Unit @Entity class and have objects.

 @Entity public class Unit extends GenericHierarchicalDictionary<Unit> {} 

A single object has a hierarchical structure , which means that Entity is related to itself, but for this I use an abstract class ( @MappedSuperclass ), so I would like to define it dynamically in this 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 works. I get an error message:

 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 this error, I have to assign targetEntity to a java class with @Entity annotation, but that is not what I would like to have.

I want @ManyToOne targetEntity to be declared dynamically from an Entity context.

How can I achieve this? Please help me.

0
source share
2 answers

Here is my hierarchical generic class (abstract, mapped superclass) implemented in spring:

 import java.util.List; import javax.persistence.CascadeType; import javax.persistence.FetchType; import javax.persistence.ManyToOne; import javax.persistence.MappedSuperclass; import javax.persistence.OneToMany; @MappedSuperclass public abstract class GenericHierarchicalModel<T extends GenericHierarchicalModel<T>> extends GenericModel<T> { private String name; @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) private T parent; @OneToMany(mappedBy = "parent") private List<T> children; public String getName() {return name;} public void setName(String name) {this.name = name;} public T getParent() {return parent;} public void setParent(T parent) {this.parent = parent;} public List<T> getChildren() {return children;} public void setChildren(List<T> children) {this.children = children;} public GenericHierarchicalModel() { super(); } public GenericHierarchicalModel(Long id) { super(id); } public GenericHierarchicalModel(Long id, String name) { super(id); this.name = name; } public GenericHierarchicalModel(Long id, String name, List<T> children) { super(id); this.name = name; this.children = children; } 

Do all the work. I have a working implementation base on this class. I hope I help.

Mark this post: Java hibernate / jpa how to create a dynamic shared object that itself is linked

0
source

I know this is a late answer, but if a solution is still needed, it goes.

This problem is related to the fact that GenericHierarchicalDictionary is an abstract class. Hibernate does not know what type of object refers to properties. Instead, you need to use the inheritance class as a type (in this case, Unit).

Thus, the property will be: -

public T parent;

instead

GenericHierarchicalDictionary public generator,

This is how I solved the same problem at our end. Hope this helps!

-1
source

Source: https://habr.com/ru/post/1207589/


All Articles