JPA / JPQL - bulk update

旧城冷巷雨未停 提交于 2019-12-05 16:56:04

You should be able to use COALESCE:

UPDATE Book b SET b.amount = COALESCE(b.amount, 1) + 1 WHERE b IN ( :books ) 

I would go and fix the nulls first with a separate query:

UPDATE Book set b.amount = 0 WHERE b.amount IS NULL

And also make it impossible to insert null, if it is not a legal value for your logic. For example have it @Column(nullable=false)

I used the following way to get the JDBC connection from the current JPA provider.

SessionImplementor si = (SessionImplementor) em.unwrap(Session.class);
Connection connection = si.getJdbcConnectionAccess().obtainConnection();
PreparedStatement pstmt = connection.prepareStatement("...");
// Do something
pstmt.addBatch();
pstmt.executeBatch();
si.getJdbcConnectionAccess().releaseConnection(connection);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!