Apply generic class @MappedSuperclass as targetEnt

2019-05-31 13:15发布

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.

2条回答
何必那么认真
2楼-- · 2019-05-31 13:48

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 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

查看更多
爷的心禁止访问
3楼-- · 2019-05-31 14:02

I know this is a late reply but if a solution is still required here goes.

This issue is because GenericHierarchicalDictionary is an abstract class. Hibernate does not know what entity type the property references. Instead what you need to do is use the inheriting class as the type (in this case Unit).

So the property would be :-

public T parent;

instead of

public GenericHierarchicalDictionary parent;

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

查看更多
登录 后发表回答