Hibernate 不通过本机查询更新值
Hibernate not updating values through a native query
亲爱的 Whosebug 社区,
我不确定为什么下面的代码没有更新我的数据库数据,但是从休眠日志中获取查询似乎有效...
我正在使用 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();
这是休眠的日志
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 在 运行s 以上的更新查询时没有更改任何数据?
非常感谢
问题是 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();
亲爱的 Whosebug 社区,
我不确定为什么下面的代码没有更新我的数据库数据,但是从休眠日志中获取查询似乎有效...
我正在使用 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();
这是休眠的日志
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 在 运行s 以上的更新查询时没有更改任何数据?
非常感谢
问题是 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();