两个实体之间的JPA OneToOne和ManyToMany

编程入门 行业动态 更新时间:2024-10-10 21:31:12
本文介绍了两个实体之间的JPA OneToOne和ManyToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我之前问了一个问题,但是我觉得还不够清楚,我会详细说明代码。 我有教师实体和部门实体 许多教师属于一个部门,而b $ b b一个部门有一名教师负责该部门。 以下是我的实施方式。

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(); ... }

和部门实体

@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我收到以下错误

@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) ...

这些需要帮助

推荐答案

这是不是JPA问题,而是由递归实例化教师 / 部门引起的。

This is not a JPA problem, but is caused by recursive instantiation of Teacher/Department.

当您创建或要求JPA创建实例时如果教师,教师尝试实例化部门 ,实例化一个教师 ...,到无穷大。

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.

因此你看到 StackOverflowError 错误接近堆栈跟踪结束。

Hence you're seeing a StackOverflowError error near the end of that stack trace.

删除 = new Teacher()和 =来自类定义的新Department()表达式;在创建它们时依赖并使用适当的setter方法。

Remove the = new Teacher() and = new Department() expressions from the class definition; depend on and use appropriate setter methods when you create them.

更多推荐

两个实体之间的JPA OneToOne和ManyToMany

本文发布于:2023-07-14 10:33:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1104119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   两个   JPA   OneToOne   ManyToMany

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!