Java 弹簧&x2B;Hibernate:具有相同标识符值的不同对象已与会话关联
在使用Spring和Hibernate的应用程序中,每次从CSV文件读取记录时,我都会解析CSV文件并通过调用Java 弹簧&x2B;Hibernate:具有相同标识符值的不同对象已与会话关联,java,hibernate,spring,dao,Java,Hibernate,Spring,Dao,在使用Spring和Hibernate的应用程序中,每次从CSV文件读取记录时,我都会解析CSV文件并通过调用handleRow()来填充db 我的域模型: “家族”有许多“亚家族” “亚家族”有许多“基因座” “位点”属于“物种” 家族亚家族轨迹都是双向映射 代码: 使用以下方法将一个物种分配到一个位点,该方法仅访问DAO层: private void assignSpecies (CommonService serv, Locus locus) { String locusId = lo
handleRow()
来填充db
我的域模型:
“家族”有许多“亚家族”
“亚家族”有许多“基因座”
“位点”属于“物种”
家族亚家族轨迹
都是双向映射
代码:
使用以下方法将一个物种分配到一个位点,该方法仅访问DAO层:
private void assignSpecies (CommonService serv, Locus locus) {
String locusId = locus.getLocusId();
String speciesId = CommonUtils.getLocusSpecies(locusId, ctx.getSpeciesList()).getSpeciesId();
//Simply get Species object from DAO
Species sp = serv.getSpeciesBySpeciesId(speciesId);
locus.setSpecies(sp);
}
Hibernate出现以下错误:
[INFO] Starting scheduled refresh cache with period [5000ms]
Hibernate: insert into species (species_id, name) values (?, ?)
Hibernate: insert into species (species_id, name) values (?, ?)
Hibernate: insert into species (species_id, name) values (?, ?)
############################ROW#####################1
SubFamiyID#######RIF0005913
Hibernate: select this_.id as id1_0_, this_.family_id as family2_1_0_, this_.rif_iD as rif3_1_0_, this_.family_name as family4_1_0_, this_.family_ipr_id as family5_1_0_, this_.family_pattern as family6_1_0_ from family this_ where this_.family_id=?
Creating NEW SubFamiyID#######RIF0005913
Hibernate: select this_.id as id3_0_, this_.species_id as species2_3_0_, this_.name as name3_0_ from species this_ where this_.species_id=?
Hibernate: insert into family (family_id, rif_iD, family_name, family_ipr_id, family_pattern) values (?, ?, ?, ?, ?)
Hibernate: insert into subfamily (sub_family_id, rif_iD, sub_family_name, sub_family_ipr_id, sub_family_pattern, family_id, sub_family_index) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into locus (locus_id, refTrans_id, function, species_id, sub_family_id, sub_family_index) values (?, ?, ?, ?, ?, ?)
Hibernate: update species set species_id=?, name=? where id=?
Hibernate: update subfamily set family_id=?, sub_family_index=? where id=?
Hibernate: update locus set sub_family_id=?, sub_family_index=? where id=?
############################ROW#####################2
SubFamiyID#######RIF0005913
Hibernate: select this_.id as id1_0_, this_.family_id as family2_1_0_, this_.rif_iD as rif3_1_0_, this_.family_name as family4_1_0_, this_.family_ipr_id as family5_1_0_, this_.family_pattern as family6_1_0_ from family this_ where this_.family_id=?
Hibernate: select subfamilie0_.family_id as family7_1_, subfamilie0_.id as id1_, subfamilie0_.sub_family_index as sub8_1_, subfamilie0_.id as id0_0_, subfamilie0_.sub_family_id as sub2_0_0_, subfamilie0_.rif_iD as rif3_0_0_, subfamilie0_.sub_family_name as sub4_0_0_, subfamilie0_.sub_family_ipr_id as sub5_0_0_, subfamilie0_.sub_family_pattern as sub6_0_0_, subfamilie0_.family_id as family7_0_0_ from subfamily subfamilie0_ where subfamilie0_.family_id=?
Hibernate: select locuslist0_.sub_family_id as sub5_1_, locuslist0_.id as id1_, locuslist0_.sub_family_index as sub7_1_, locuslist0_.id as id2_0_, locuslist0_.locus_id as locus2_2_0_, locuslist0_.refTrans_id as refTrans3_2_0_, locuslist0_.function as function2_0_, locuslist0_.sub_family_id as sub5_2_0_, locuslist0_.species_id as species6_2_0_ from locus locuslist0_ where locuslist0_.sub_family_id=?
Hibernate: select species0_.id as id3_0_, species0_.species_id as species2_3_0_, species0_.name as name3_0_ from species species0_ where species0_.id=?
Hibernate: select this_.id as id1_0_, this_.family_id as family2_1_0_, this_.rif_iD as rif3_1_0_, this_.family_name as family4_1_0_, this_.family_ipr_id as family5_1_0_, this_.family_pattern as family6_1_0_ from family this_ where this_.family_id=?
Hibernate: select this_.id as id3_0_, this_.species_id as species2_3_0_, this_.name as name3_0_ from species this_ where this_.species_id=?
Exception in thread "main" [INFO] Closing Compass [compass]
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.bigg.nihonbare.common.domain.Species#1]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.bigg.nihonbare.common.domain.Species#1]
Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.bigg.nihonbare.common.domain.Species#1]
at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
任何提示?使用
merge()
。异常意味着当前会话已经知道您正在传递的实体。如果没有,请检查您是如何重写hashCode()
和equals()
-它应该为不同的实体返回不同的值。如果您正在执行delete()
或update()
,也可能会遇到此问题。如果您自己构建hibernate映射的pojo(可能是从DTO构建),则可能会出现问题。此pojo现在与已在会话中的pojo具有相同的标识符,这导致了问题
你现在有两个选择。要么照@Bozho说的做,然后首先merge()
对象。它负责更新。要删除,请获取merge()
返回的对象并将其删除
另一个选项是首先使用对象的id查询会话
,然后删除或更新它。当实体的id列没有GeneratedValue注释时,我看到了这一点:
@GeneratedValue(strategy = GenerationType.AUTO)
我决定:
关于删除方法:
this.getHibernateTemplate().clear();
this.getHibernateTemplate().delete(obj);
// Esta línea realiza el "commit" del comando
this.getHibernateTemplate().flush();
this.getHibernateTemplate().merge(obj);
// Esta línea realiza el "commit" del comando
this.getHibernateTemplate().flush();
关于更新方法:
this.getHibernateTemplate().clear();
this.getHibernateTemplate().delete(obj);
// Esta línea realiza el "commit" del comando
this.getHibernateTemplate().flush();
this.getHibernateTemplate().merge(obj);
// Esta línea realiza el "commit" del comando
this.getHibernateTemplate().flush();
如果在saveOrUpdate()调用后更新对象execute()并将其从会话中逐出,还应检查对象的哈希代码实现。您可能已经创建了两个会话的实例
Session session = factory.openSession();
如果您在一个函数中打开了一个会话,并通过创建另一个会话来执行另一个函数,则会出现此问题。这发生在我身上,因为我的部分复合键为null。例:
@Id
@Column(name = "id")
private String id;
@JoinColumn(name = "id")
private Username username;
用户名恰好为null,这导致“重复”null主键,即使id不同。是否有方法使用会话。saveOrUpdate
更新子实体持久性。我已经使用了get()
,我需要用给定的ID更新子实体。merge正在工作,但只是为了获取信息,也可以阅读本文:谢谢!我在删除实体时遇到问题。对我来说非常有效。谢谢