【发布时间】:2015-06-24 07:06:04
【问题描述】:
我正在开发一个 Spring + Hibernate 应用程序,一切正常。制作方法我发现了一个我无法真正解释的奇怪行为,所以我会告诉你我得到了什么,也许我们会找到解决方案。
此方法检索解析网页的足球运动员列表,我尝试查找数据库中是否已有同名球员;如果我已经拥有它,我会设置一些参数并更新该对象。如果我没有具有该名称的播放器,我想插入它。我显然不能使用 saveOrUpdate 方法,因为我解析的对象没有 id,因为我没有从数据库中检索它们。
这是产生错误的代码sn-p(在Service层,然后声明为Transactional):
List<Calciatore> calciatoriAggiornati = PopolaDbCalciatori.getListaCalciatori(imagesPath);
for(Calciatore calciatore: calciatoriAggiornati){
Calciatore current = calciatoreDao.getCalciatoreByNome(calciatore.getNome());
if( current != null){
current.setAttivo(true);
current.setRuolo(calciatore.getRuolo());
current.setUrlFigurina(calciatore.getUrlFigurina());
current.setSquadraReale(calciatore.getSquadraReale());
calciatoreDao.update(current);
}
else{
calciatore.setAttivo(true);
calciatoreDao.insert(calciatore);
}
}
return true;
}
getCalciatoreByName 方法如下(单独使用时有效):
public Calciatore getCalciatoreByNome(String nomeCalciatore) {
List<Calciatore> calciatori = getSession().createCriteria(Calciatore.class)
.add(Restrictions.eq("nome",nomeCalciatore)).list();
return calciatori.size() == 0? null : calciatori.get(0);
}
BaseDaoImpl 类继承的 insert 方法在独立使用时也可以工作,如下所示:
public Boolean insert(T obj) throws DataAccessException {
getSession().save(obj);
return true;
}
结果很奇怪:列表的第一个对象通过getCalciatoreByNome方法没有问题;因为我在数据库上没有实例,所以流程转到插入。第一轮for结束后,这是控制台:
Hibernate:
select
this_.kid as kid1_0_3_,
this_.attivo as attivo2_0_3_,
this_.dataDiNascita as dataDiNa3_0_3_,
this_.nome as nome4_0_3_,
this_.ruolo as ruolo5_0_3_,
this_.squadraCorrente_kid as squadraC9_0_3_,
this_.squadraReale as squadraR6_0_3_,
this_.urlFigurina as urlFigur7_0_3_,
this_.version as version8_0_3_,
squadrafan2_.kid as kid1_7_0_,
squadrafan2_.attiva as attiva2_7_0_,
squadrafan2_.nome as nome3_7_0_,
squadrafan2_.utenteAssociato_kid as utenteAs5_7_0_,
squadrafan2_.version as version4_7_0_,
utente3_.kid as kid1_10_1_,
utente3_.attivo as attivo2_10_1_,
utente3_.hashPwd as hashPwd3_10_1_,
utente3_.ruolo_kid as ruolo_ki6_10_1_,
utente3_.username as username4_10_1_,
utente3_.version as version5_10_1_,
ruolo4_.kid as kid1_5_2_,
ruolo4_.nome as nome2_5_2_,
ruolo4_.version as version3_5_2_
from
Calciatore this_
left outer join
SquadraFantacalcio squadrafan2_
on this_.squadraCorrente_kid=squadrafan2_.kid
left outer join
Utente utente3_
on squadrafan2_.utenteAssociato_kid=utente3_.kid
left outer join
Ruolo ruolo4_
on utente3_.ruolo_kid=ruolo4_.kid
where
this_.nome=?
Hibernate:
call next value for SEQ_CALCIATORE
正如您所见,没有引发异常,但行为已经受到影响,因为没有真正执行插入!最后一行日志只显示序列生成器!
在for 循环的第二轮,随着流程接近getCalciatoreByNome 方法,这是控制台日志:
Hibernate:
insert
into
Calciatore
(attivo, dataDiNascita, nome, ruolo, squadraCorrente_kid, squadraReale, urlFigurina, version, kid)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)
24/06/2015 09:03:27 - INFO - (AbstractBatchImpl.java:208) - HHH000010: On release of batch it still contained JDBC statements
24/06/2015 09:03:27 - WARN - (SqlExceptionHelper.java:144) - SQL Error: -5563, SQLState: 42563
24/06/2015 09:03:27 - ERROR - (SqlExceptionHelper.java:146) - incompatible data type in operation
24/06/2015 09:03:39 - DEBUG - (AbstractPlatformTransactionManager.java:847) - Initiating transaction rollback
哇,真奇怪。当我尝试第二次执行 select 方法时,Hibernate 尝试使insert 生成我无法在任何地方真正找到的错误,并开始生成回滚\异常。
我尝试尽可能多地调试,但我无法真正理解发生了什么,因为当我以独立方式执行这些操作时,一切似乎都正常。
有什么建议吗?
【问题讨论】:
-
这孩子可以是实体类的
null吗? -
孩子是由序列生成的,所以当我进行插入时,如果孩子为空也没有问题。我现在在问题上附上Calciatory 实体类。
-
是的!请将
Hibernate: call next value for SEQ_CALCIATORE-Log 从 Select-Log 右侧移至 Insert-Log。 -
如果您发布实体,请同时发布表格的DDL。我猜它的属性类型与字段类型或数据库中的序列不匹配。
-
这就是休眠的工作原理。执行选择时,所有未决更改都将刷新到数据库,以便当前事务可以看到它们。因此,在进行下一次选择之前,将执行挂起的插入/更新。对序列进行选择是因为保存(或持久化)需要知道要插入的对象的 id,以便在需要时插入。
标签: java sql database spring hibernate