使用已部署在Jboss 4上的現有工作Hibernate 3代碼,現在我試圖將它部署在Wildfly 10(Hibernate 5)上。在部署時進行驗證,將顯示以下信息:在Hibernate中,兩個實體如何能夠與第三個實體具有多對多的單向關係?
org.hibernate.persister.walking.spi.WalkingException: Association has already been visited: AssociationKey(table=pur_DemandDtl_DemandHeader, columns={DemandEventHeaderTRIk})
(我下方會顯示出完整的堆棧)
首先,我將給出一個總結: 有涉及3類。在2個類中有一個註釋爲ManyToMany的成員集,它是單向的。該集有第三類的成員。
有3個表分別對應3個類別。 另外還有一個表示兩個關聯的第四個表。 這第四張桌子有3列。每列對應於其中一個表格。 顯然,在每一行中,與這兩個表關聯的兩列中的一列有一個空值。
我幾乎沒有Hibernate的經驗,但它看起來每個關係的定義都與我見過的所有示例完全相同。我腦海中唯一的問題是:1)我是否還需要做其他事情,因爲與第三方有關係的是兩張桌子? 2)將兩個關聯放在同一個關聯表中是合法的嗎? 3)是前兩類問題的層次結構?
好的,現在我將顯示一些代碼。我會省略一些細節,如果需要,稍後會添加。
前兩個類是PromoDtl和ReserveDtl。它們共享一個共同的超類PromoReserveDtl,並且每個與第三個類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",
[email protected](name="PromoDtlIk"),
[email protected](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",
[email protected](name="ReserveDtlIk"),
[email protected](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() {
//...
}
}
的關聯表的定義如下:
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$1$1.run(PersistenceUnitServiceImpl.java:179)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:121)
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:667)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.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$1$1.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
感謝您的時間。
米蘭達,我檢查了這是正確的答案,因爲我拆表,現在這個問題已經一去不復返了。然而,我很難相信兩個實體之間的關係必須在一個排他性的表格中,並且不能僅僅使用一個更一般的表格的2列或3列來實現呢?是的,我期望hibernate過濾出空值(因爲null不是實體)。這就是它在JBoss 4.2.3和Hibernate 3中的情況....在將它遷移到Wildfly 10之前,這是工作代碼。但是,謝謝你,至少我得到了解決問題的答案。 – inor