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

时间:2018-09-08 17:18:15

标签: hibernate jpa kotlin spring-data-jpa

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

当我尝试坚持时,主要问题到来了。当我检查数据库,但表user_relationship无法正常工作时,该关系的“关系”端似乎工作正常。

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

编辑:我看过该帖子,建议将其重复。在接受的答案中,Sym-sym指出了固定的关系并提供了示例。我已经研究了示例,但是在关系的OneToMany端看到的唯一区别是maptedBy属性。我不太确定要映射哪一个,因为在关系的另一端有两个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

1 个答案:

答案 0 :(得分:0)

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

到目前为止,针对我们的特定情况找到的两种解决方案是:

  • 分别在实体的每个中将两个单独的字段相互映射。不仅一个对象中的一个字段到另一个对象中的两个字段。
  • 或者,使用一个字段@ManyToMany映射对象,并找出另一种方法来确定谁拥有哪个状态。(例如最大的ID或类似的东西)

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