将同一JPA实体多次映射到其他JPA实体

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将同一JPA实体多次映射到其他JPA实体相关的知识,希望对你有一定的参考价值。

我想通过first_user和second_user将User映射到Relationship,以便我可以跟踪他们的关系状态。可能有更好的方法,我很乐意考虑任何有用的建议。

当我试图坚持时,主要问题到来了。当我检查数据库但表user_relationship不起作用时,关系的“关系” - 似乎工作得很好。

所以问题基本上是为什么不是用户工作中的持久关系?

编辑:我已经看到了这个被建议重复的帖子。在接受的答案中,Sym-sym指出修复关系并提供示例。我查看了这些示例,但我在关系的OneToMany端看到的唯一区别是mappedBy属性。我不确定要映射哪一个,因为我们在关系的另一边有两个ManyToOne。我们会通过firstParty或者secondParty进行映射吗?另一个会发生什么?

这是我到目前为止所得到的:

@Entity
@Table(name = "user")
data class User(

    @Id
    @GeneratedValue(generator = "my_user_sequence")
    @SequenceGenerator(name = "my_user_sequence", sequenceName = "user_sequence", allocationSize = 1)
    val id: Long = -1,

    @JsonIgnore
    @OneToOne(mappedBy = "user", cascade = [CascadeType.ALL], optional = false)
    var preferences: Preferences? = null,

    @OneToMany(cascade = [CascadeType.ALL])
    var relationships: MutableSet<Relationship> = mutableSetOf(),

    @Column(name = "facebook_id")
    var fbId: String = "",
    var email: String? = null,

    var registrationToken: String = "",

    var birthday: LocalDate? = null,

    val created: LocalDateTime = LocalDateTime.now(Clock.systemUTC()),

    @Column(name = "phone_number")
    var phoneNumber: String = "",
    var password: String? = null,

    @Column(name = "first_name")
    var firstName: String? = null,

    @Column(name = "show_location")
    var showLocation: Boolean = true,

    @Column(name = "show_age")
    var showAge: Boolean = true,

    @Column(name = "picture_url")
    var pictureUrl: String? = null,

    @Column(name = "notify_message")
    var notifyMessage: Boolean = true,

    @Column(name = "notify_relationships")
    var notifyRelationship: Boolean = true
)

这是关系类:

@Entity
@Table(name = "relationship")
data class Relationship(

        @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "joyship_relationship_sequence")
    @SequenceGenerator(name = "joyship_relationship_sequence", sequenceName = "relationship_sequence", allocationSize = 1)
    val id: Long = -1,

    @ManyToOne(cascade = [])
    @JoinTable(name = "first_party")
    val firstParty: User,

    @ManyToOne(cascade = [])
    @JoinTable(name = "second_party")
    val secondParty: User,

    // Id of user is key for relationship_status of user with smaller number on id
    @Enumerated(EnumType.STRING)
    @Column(name = "first_status")
    var firstStatus: RelationshipStatus = NOT_INTRODUCED,

    // Id of user is key for relationship_status of user with bigger number on id
    @Enumerated(EnumType.STRING)
    @Column(name = "second_status")
    var secondStatus: RelationshipStatus = NOT_INTRODUCED,

    // Changes when relationship_status_1 or *_2 changes
    // (Strongest of the statuses dictates common_relationship_status)
    @Enumerated(EnumType.STRING)
    @Column(name = "common_relationship_status")
    var commonRelationshipStatus: RelationshipStatus = NOT_INTRODUCED,

    @Column(name = "relative_distance")
    var relativeDistance: Int = 160,

    @Column
    val established: LocalDateTime = LocalDateTime.now(Clock.systemUTC()),

    @OneToOne(mappedBy = "relationship", cascade = [CascadeType.ALL], optional = false)
    @JoinColumn(name = "last_interaction")
    var lastInteraction: Interaction? = null,

    @Column
    var lastInteractionTime: LocalDateTime = LocalDateTime.now(Clock.systemUTC()))

当我尝试通过关系持久保存用户时,它始终给我这个:

    javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: co.joyship.server.fraendurheim.models.Relationship

    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:814)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:774)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener$1.cascade(JpaPersistEventListener.java:80)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:467)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:392)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193)
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:500)
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:432)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:395)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:126)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:445)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:281)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:783)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:768)
    at org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager.persist(TestEntityManager.java:93)
    at co.joyship.server.fraendurheim.RelationshipRepositoryTest.whenRequestingUserRelationshipFromDBReturnCorrectPagination(RelationshipRepositoryTest.kt:64)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: co.joyship.server.fraendurheim.models.Relationship
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:807)
    ... 52 more
答案

如果有人发现自己处于和我一样的境地:不要尝试这种方法。就我的试验所示,不可能在一个实体中的两个@ManyToOne与另一个实体中的一个@OneToMany之间创建双向关系。

到目前为止,我们针对具体案例找到的两个解决方案是:

  • 将每个实体中的两个单独字段映射到彼此。不只是一个对象中的一个字段到另一个对象中的两个字段。
  • 或者,使用一个字段@ManyToMany映射对象,并找出确定谁拥有哪个状态的另一种方法。(像最大的ID或类似的东西)

我使用this引用来使@OneToMany关系正确

以上是关于将同一JPA实体多次映射到其他JPA实体的主要内容,如果未能解决你的问题,请参考以下文章

JPA,如何使用同一个类(实体)来映射不同的表?

JPA:如何将一些实体映射到另一个数据库实例的不同模式?

如何在同一个数据库表上映射两个 JPA 或 Hibernate 实体

基于其他两个实体映射相关的 JPA 实体映射

如何使用 JPA 和 Hibernate 将 MySQL JSON 列映射到 Java 实体属性

JPA一对多映射