Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 休眠选择并插入奇怪行为_Java_Sql_Database_Spring_Hibernate - Fatal编程技术网

Java 休眠选择并插入奇怪行为

Java 休眠选择并插入奇怪行为,java,sql,database,spring,hibernate,Java,Sql,Database,Spring,Hibernate,我正在开发一个Spring+Hibernate应用程序,一切都很好。做一个方法我发现了一个奇怪的行为,我真的无法解释,所以我会告诉你我得到了什么,也许我们会找到一个解决办法 此方法检索解析网页的足球运动员列表,我尝试查找数据库中是否已有同名球员;如果我已经有了它,我会设置一些参数并更新该对象。如果我没有这个名字的球员,我想插入它。我显然不能使用saveOrUpdate方法,因为我解析的对象没有id,因为我没有从数据库中检索它们 这是生成错误的代码段(它位于服务层,然后声明为事务性): 结果很奇怪

我正在开发一个Spring+Hibernate应用程序,一切都很好。做一个方法我发现了一个奇怪的行为,我真的无法解释,所以我会告诉你我得到了什么,也许我们会找到一个解决办法

此方法检索解析网页的足球运动员列表,我尝试查找数据库中是否已有同名球员;如果我已经有了它,我会设置一些参数并更新该对象。如果我没有这个名字的球员,我想插入它。我显然不能使用
saveOrUpdate
方法,因为我解析的对象没有id,因为我没有从数据库中检索它们

这是生成错误的代码段(它位于服务层,然后声明为
事务性
):

结果很奇怪:列表的第一个对象毫无问题地通过了方法
getCalciatoreByNome
;由于数据库中没有实例,因此流将转到insert。
的第一轮
结束后,这是控制台:

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
生成我在任何地方都找不到的错误,然后开始回滚\异常生成

我试着尽可能多地调试,但我不能真正理解发生了什么,因为当我以独立方式执行这些操作时,一切似乎都正常

有什么建议吗?

使用时,当前挂起的更改将在以下情况下刷新:

  • 事务提交
  • 执行查询
当您发出
insert
命令时,Hibernate只在操作队列中添加一个
EntityInsertAction
,但它不会


您看到在第二个迭代周期执行insert的原因是select查询触发刷新。

在实体类中kid是否可以
null
?kid是由序列生成的,因此当我进行插入时,如果kid为null,则没有问题。我现在附上关于这个问题的钙化实体类。所以是的!请将SEQ_CALCIATORE的
Hibernate:call next值从Select Log右侧移动到Insert-Log。如果发布实体,请也发布表的DDL。我猜是属性的类型与数据库中字段或序列的类型不匹配。这就是hibernate的工作方式。执行选择操作时,所有挂起的更改都会刷新到数据库中,以便当前事务可以看到它们。因此,在执行下一次选择之前,将执行挂起的插入/更新。选择序列是因为保存(或持久化)需要知道要插入的对象的id,以便在需要时插入。
public Boolean insert(T obj) throws DataAccessException {
        getSession().save(obj);
        return true;
    }
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
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