hibernate不通过本机查询更新值

lnlaulya  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(447)

亲爱的stackoverflow社区,
我不知道为什么下面的代码不更新我的数据库数据,但是从hibernate日志中获取查询似乎是可行的。。。
我使用的是hibernate 5.2.12.final
这就是我的全部职能:

EntityManager em = Utils.initEntityManager();
    em.getTransaction().begin();

    em.persist(receptacle);

    em.createNativeQuery("UPDATE product_item pi"
            + " JOIN ereturn er ON pi.ereturn = er.id "
            + "SET pi.receptacle = :receptacle "
            + "WHERE pi.returnAction = :returnAction "
            + "AND er.destination = :destination "
            + "AND er.status = 'RECEIVED'")
            .setParameter("receptacle", receptacle.getId())
            .setParameter("returnAction", receptacle.getReturnAction())
            .setParameter("destination", receptacle.getDestination().getId())
            .executeUpdate();       

    em.getTransaction().commit();
    em.close();

这是hibernate的日志

19:47:29,048 DEBUG [org.hibernate.SQL] - 
    UPDATE
        product_item pi 
    JOIN
        ereturn er 
            ON pi.ereturn = er.id 
    SET
        pi.receptacle = ? 
    WHERE
        pi.returnAction = ? 
        AND er.destination = ? 
        AND er.status = 'RECEIVED'
Hibernate: 
    UPDATE
        product_item pi 
    JOIN
        ereturn er 
            ON pi.ereturn = er.id 
    SET
        pi.receptacle = ? 
    WHERE
        pi.returnAction = ? 
        AND er.destination = ? 
        AND er.status = 'RECEIVED'
19:47:29,049 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [1] as [BIGINT] - [20]
19:47:29,049 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [3] as [BIGINT] - [148375]
19:47:29,049 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - binding parameter [2] as [VARBINARY] - [RETURNTOCLIENT]

我从日志中获取相同的查询,替换参数并在mysql控制台中运行它。。。查询工作!!!
问:当上面的更新查询运行时,为什么mysql不更改任何数据?
非常感谢你

uqcuzwp8

uqcuzwp81#

问题是returnaction字段是一个枚举,所以我必须将其转换为字符串。
奇怪的是hibernate没有抱怨。。。
解决方法如下:

em.createNativeQuery("UPDATE product_item pi"
            + " JOIN ereturn er ON pi.ereturn = er.id "
            + "SET pi.receptacle = :receptacle "
            + "WHERE pi.returnAction = :returnAction "
            + "AND er.destination = :destination "
            + "AND er.status = 'RECEIVED'")
            .setParameter("receptacle", receptacle.getId())
            .setParameter("returnAction", receptacle.getReturnAction().toString())
            .setParameter("destination", receptacle.getDestination().getId())
            .executeUpdate();

相关问题