我在下面创建了两个表。
TableOne
id pk
code varchar (references a column in TableTwo, non unique)
TableTwo
id pk
code varchar (referenced in TableOne, non unique)
option
在表1中,一个id只能有一个代码。在表2中,代码可以重复以用于不同的选项。即代码+选项将是唯一的(不是我在DB中定义了约束)。
现在,我有我的实体类,如下所述
@Entity
@Table(name = "TableOne")
public class Table1 {
@Id
@Column(name = "id")
private String id;
@OneToMany(mappedBy = "table1", fetch = FetchType.EAGER)
private List<Table2> table2s;
}
@Entity
@Table(name = "TableTwo")
public class Table2 {
@Id
@Column(name = "id")
private String id;
@ManyToOne
@JoinColumn(name = "code", referencedColumnName = "code", nullable = false, insertable = false, updatable = false)
private Table1 table1;
}
当我尝试执行我的应用程序时,它会出现以下错误
org.springframework.orm.jpa.JpaSystemException: Found shared references to a collection: model.Table1.table2s; nested exception is org.hibernate.HibernateException: Found shared references to a collection: model.Table1.table2s
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761) [spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) [spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504) [spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292) [spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) [spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) [spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at au.th.api.assessment.service.MasterQuestionsServiceImpl$$EnhancerBySpringCGLIB$$e50ce6a4.getQuestionById(<generated>) [classes/:na]
TableOne中有两行具有相同的代码。 TableTwo只有一行代码。
有人可以帮忙吗?
修改
如果我删除 referencedColumnName ,它可以正常工作,但列表为空。
在调试时,我可以看到列表对象在提交事务之前被提取为PersistentBag。在提交时,它会抛出异常。异常之前打印的错误是
o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [org.hibernate.HibernateException: Found shared references to a collection: model.Table1.table2s]
答案 0 :(得分:0)
您对OneToMany关系有误。请参阅下面的示例。
@Entity
@Table(name = "course")
public class Course implements Serializable{
private int id;
private Set<Student> students;
public Course(){
}
@OneToMany(mappedBy = "course", cascade = CascadeType.ALL)
public Set<Student> getStudents() {
return students;
}
}
@Entity
public class Student implements Serializable{
private int id;
private Course course;
@ManyToOne
@JoinColumn(name = "course_id")
public Course getCourse() {
return course;
}
}
您可以在帖子OneToMany relationship
中找到工作示例