简体   繁体   English

两个实体之间的JPA OneToOne和ManyToMany

[英]JPA OneToOne and ManyToMany between two entities

I asked a question earlier, but think it wasn't clear enough i will elaborate further with the code. 我早些时候问了一个问题,但认为还不够清楚,我将不进一步阐述代码。
I have a Teacher entity and Department entity 我有一个教师实体和一个部门实体
Many Teachers belongs to One Department and 许多老师属于一个系
One Department has One Teacher who heads the Department. 一个系有一名系主任。
Here is how I am implementing it. 这是我实现它的方式。

@Entity
public class Teacher extends Model {
@Required
public String surname;

@Required
public String othernames;

    ... 

@OneToOne(mappedBy = "deptHead")
public Department headedBy = new Department();

@ManyToOne(cascade=CascadeType.ALL)
public Department dept = new Department();

... 
}

and the Department entity 和部门实体

@Entity
public class Department extends Model {
@Required
public String deptName; 

@OneToMany(mappedBy="dept")
public List<Teacher> teachers = new ArrayList<Teacher>();

@OneToOne
public Teacher deptHead = new Teacher();

    ...

}

I am getting the error below 我收到以下错误

    @6c4nj8nmg
    Internal Server Error (500) for request GET /

    JPA error
    A JPA error occurred (Unable to build EntityManagerFactory): could not instantiate   test objectmodels.Department

    play.exceptions.JPAException: Unable to build EntityManagerFactory
    at play.db.jpa.JPAPlugin.onApplicationStart(JPAPlugin.java:269)
    at play.plugins.PluginCollection.onApplicationStart(PluginCollection.java:525)
    at play.Play.start(Play.java:526)
    at play.Play.detectChanges(Play.java:630)
    at play.Invoker$Invocation.init(Invoker.java:198)
    at Invocation.HTTP Request(Play!)
    Caused by: org.hibernate.InstantiationException: could not instantiate test objectmodels.Department
    at org.hibernate.engine.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:48)
    at org.hibernate.engine.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:67)
    at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:67)
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:135)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:485)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:133)
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)
    at play.db.jpa.JPAPlugin.onApplicationStart(JPAPlugin.java:267)
    ... 5 more
    Caused by: java.lang.reflect.InvocationTargetException
    at  org.hibernate.engine.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:45)
    ... 15 more
     Caused by: java.lang.StackOverflowError
    at java.lang.Class.searchMethods(Unknown Source)
    at java.lang.Class.getMethod0(Unknown Source)
    at java.lang.Class.getMethod(Unknown Source)
    at      play.classloading.enhancers.PropertiesEnhancer$FieldAccessor.invokeWriteProperty(PropertiesEnhancer.java:268)
    at models.Department.<init>(Department.java:23)
    at models.Teacher.<init>(Teacher.java:47)
    at models.Department.<init>(Department.java:26)
    ...

Needs help on these 在这些方面需要帮助

This is not a JPA problem, but is caused by recursive instantiation of Teacher / Department . 这不是JPA问题,而是由Teacher / Department递归实例化引起的。

When you create, or ask JPA to create, an instance of Teacher , the Teacher attempts to instantiate a Department , which instantiates a Teacher ..., to infinity. 当您创建或要求JPA创建Teacher的实例时, Teacher尝试实例化Department ,实例化Teacher ...到无穷大。

Hence you're seeing a StackOverflowError error near the end of that stack trace. 因此,您会在该堆栈跟踪的结尾附近看到一个StackOverflowError错误。

Remove the = new Teacher() and = new Department() expressions from the class definition; 从类定义中删除= new Teacher()= new Department()表达式; depend on and use appropriate setter methods when you create them. 创建它们时,请依赖并使用适当的setter方法。

I removed the @oneToOne relationship between Teacher and Department and created a new entity called DepartmentHeads, everything now works fine. 我删除了Teacher和Department之间的@oneToOne关系,并创建了一个名为DepartmentHeads的新实体,现在一切正常。
Here is the Results. 这是结果。

@Entity
public class Teacher extends Model {
    @Required
    public String surname;

    @Required
    public String othernames;

    ... 

    @ManyToOne(cascade=CascadeType.ALL)
    public Department dept = new Department();

    ... 
}

@Entity
public class Department extends Model {
    @Required
    public String deptName; 

    @OneToMany(mappedBy="dept")
    public List<Teacher> teachers = new ArrayList<Teacher>();

    ...

}

@Entity
public class DepartmentHead extends Model{
    @OneToOne
    public Teacher teacher = new Teacher();

    @OneToOne
    public Department dept = new Department();  
}

Everything now works fine. 现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM