在 Hibernate 中,两个实体如何与第三个实体建立多对多单向关系?

In Hibernate how can two entities have many-to-many unidirectional relationship to a third entity?

使用部署在 Jboss 4 上的现有工作 Hibernate 3 代码,现在我正尝试将它部署在 Wildfly 10 (Hibernate 5) 上。在部署时完成验证期间,显示以下消息:

org.hibernate.persister.walking.spi.WalkingException: Association has already been visited: AssociationKey(table=pur_DemandDtl_DemandHeader, columns={DemandEventHeaderTRIk})

(我将在下面展示完整的堆栈)

首先我总结一下: 涉及 3 classes。在 2 classes 中有一个成员集注释为 ManyToMany 并且它是单向的。该集合有第 3 个 class.

的成员

3个class分别对应3个table。 此外还有第 4 个 table 代表两个协会。 第 4 个 table 有 3 列。每列对应于 table 之一。 显然,在每一行中,与两个 table 关联的两列之一具有空值。

我几乎没有使用 Hibernate 的经验,但看起来每个关系的定义都与我见过的所有示例完全相同。我脑子里唯一的问题是:1)我是否必须做其他事情,因为有 2 个 table 与第三个有关系? 2) 在同一个协会中持有两个协会是否合法 table? 3) 前 2 个 class 的层次结构有问题吗?

好的,现在我将展示一些代码。我会省略一些细节,如果需要的话稍后会添加。

前 2 个 class 是 PromoDtl 和 ReserveDtl。它们共享一个公共的 superclass PromoReserveDtl,并且每个都与第三个 class DemandEventHeaderTriggerRecord.

具有单向的 ManyToMany 关系
@MappedSuperclass
public abstract class PromoReserveDtl implements Serializable,Comparable<PromoReserveDtl>{

    private Integer promoReserveDtlIk;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "PromoReserveDtlIk")
    public Integer getPromoReserveDtlIk() {
        return promoReserveDtlIk;
    }
    protected Set<DemandEventHeaderTriggerRecord> demandEventHeaderTriggerRecord = new HashSet<DemandEventHeaderTriggerRecord>();
    public void setDemandEventHeaderTriggerRecord(Set<DemandEventHeaderTriggerRecord> demandEventHeaderTriggerRecord) {
        this.demandEventHeaderTriggerRecord = demandEventHeaderTriggerRecord;
    }
    // other stuff
}  

@Entity
@Table(name="pur_PromoDtl")
public class PromoDtl extends PromoReserveDtl implements Serializable{
    @ManyToMany(fetch = FetchType.EAGER,cascade = { CascadeType.ALL })
    @JoinTable(name="pur_DemandDtl_DemandHeader",
          joinColumns=@JoinColumn(name="PromoDtlIk"),
          inverseJoinColumns=@JoinColumn(name="DemandEventHeaderTRIk"))
    public Set<DemandEventHeaderTriggerRecord> getDemandEventHeaderTriggerRecord() {
        return demandEventHeaderTriggerRecord;
    }
    // other stuff
}

@Entity
@Table(name="pur_ReserveDtl")
public class ReserveDtl extends PromoReserveDtl implements Serializable {
    @ManyToMany(fetch = FetchType.EAGER,cascade = { CascadeType.ALL })
    @JoinTable(name="pur_DemandDtl_DemandHeader",
          joinColumns=@JoinColumn(name="ReserveDtlIk"),
          inverseJoinColumns=@JoinColumn(name="DemandEventHeaderTRIk"))
    public Set<DemandEventHeaderTriggerRecord> getDemandEventHeaderTriggerRecord() {
        return demandEventHeaderTriggerRecord;
    }
    // other stuff
}

@Entity
@Table(name="pur_DemandEventHeaderTriggerRecord")
public class DemandEventHeaderTriggerRecord  implements Serializable{
    private Integer demandEventHeaderTRIk;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "DemandEventHeaderTRIk")
    public Integer getDemandEventHeaderTRIk() {
        return demandEventHeaderTRIk;
    }
    public boolean equals(Object other) {
        //...
    }
    public int hashCode() {
        //...
    }
}

关联table定义如下:

CREATE TABLE [dbo].[pur_DemandDtl_DemandHeader](
    [PromoDtlIk] [int] NULL,
    [ReserveDtlIk] [int] NULL,
    [DemandEventHeaderTRIk] [int] NOT NULL
) ON [PRIMARY]

部署时记录的完整堆栈跟踪如下:

2016-11-22 14:51:07,765 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 84) MSC000001: Failed to start service jboss.persistenceunit."InSyncEar-11.0.0-SNAPSHOT.ear/PurchasingServices.jar#purchasingpersistence": org.jboss.msc.service.StartException in service jboss.persistenceunit."InSyncEar-11.0.0-SNAPSHOT.ear/PurchasingServices.jar#purchasingpersistence": javax.persistence.PersistenceException: [PersistenceUnit: purchasingpersistence] Unable to build Hibernate SessionFactory
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.run(PersistenceUnitServiceImpl.java:179)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.run(PersistenceUnitServiceImpl.java:121)
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:667)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.run(PersistenceUnitServiceImpl.java:193)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: purchasingpersistence] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882)
at org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.run(PersistenceUnitServiceImpl.java:161)
... 7 more
Caused by: org.hibernate.persister.walking.spi.WalkingException: Association has already been visited: AssociationKey(table=pur_DemandDtl_DemandHeader, columns={DemandEventHeaderTRIk})
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.addAssociationKey(MetamodelGraphWalker.java:281)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitCollectionElements(MetamodelGraphWalker.java:257)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitCollectionDefinition(MetamodelGraphWalker.java:208)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitAssociation(MetamodelGraphWalker.java:185)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitAttributeDefinition(MetamodelGraphWalker.java:160)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitAttributes(MetamodelGraphWalker.java:131)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitEntityDefinition(MetamodelGraphWalker.java:94)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitCollectionElements(MetamodelGraphWalker.java:264)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitCollectionDefinition(MetamodelGraphWalker.java:208)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitAssociation(MetamodelGraphWalker.java:185)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitAttributeDefinition(MetamodelGraphWalker.java:160)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitAttributes(MetamodelGraphWalker.java:131)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitEntityDefinition(MetamodelGraphWalker.java:94)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitAssociation(MetamodelGraphWalker.java:188)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitAttributeDefinition(MetamodelGraphWalker.java:160)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitAttributes(MetamodelGraphWalker.java:131)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitEntityDefinition(MetamodelGraphWalker.java:94)
at org.hibernate.persister.walking.spi.MetamodelGraphWalker.visitEntity(MetamodelGraphWalker.java:55)
at org.hibernate.loader.plan.build.spi.MetamodelDrivenLoadPlanBuilder.buildRootEntityLoadPlan(MetamodelDrivenLoadPlanBuilder.java:39)
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.<init>(AbstractLoadPlanBasedEntityLoader.java:81)
at org.hibernate.loader.entity.plan.EntityLoader.<init>(EntityLoader.java:103)
at org.hibernate.loader.entity.plan.EntityLoader.<init>(EntityLoader.java:38)
at org.hibernate.loader.entity.plan.EntityLoader$Builder.byUniqueKey(EntityLoader.java:83)
at org.hibernate.loader.entity.plan.EntityLoader$Builder.byPrimaryKey(EntityLoader.java:77)
at org.hibernate.loader.entity.plan.AbstractBatchingEntityLoaderBuilder.buildNonBatchingLoader(AbstractBatchingEntityLoaderBuilder.java:30)
at org.hibernate.loader.entity.BatchingEntityLoaderBuilder.buildLoader(BatchingEntityLoaderBuilder.java:59)
at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:2254)
at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:2276)
at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:3876)
at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:3858)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:444)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
... 9 more

感谢您的宝贵时间。

In addition there is a 4th table which represents BOTH associations. This 4th table has 3 columns. Each column corresponds to one of the tables. Obviously in each row one of the two columns associated with the two tables has a null value.

显然 你把苹果和香蕉放在同一个 table 里。 JoinTable 的目的是准确存储一个关系。一个 JoinTable 有两个列,每边都有实体的 ID(我不知道是否可以使用复合键,那么你有相应的更多列)。 Hibernate 应该如何处理空值?过滤掉它们?或者向集合中添加一个空值?

1) do I have to do something else because there are 2 tables in a relationship with the third?

你必须把这个 table 一分为二。

2) is it legitimate to hold both associations in the same association table?

规格是否合法:不知道。但是没有意义。

3) is the hierarchy of the first 2 classes problemtic?

没有