将本机查询转换为Hibernate查询

时间:2014-07-28 06:54:00

标签: java sql hibernate postgresql pessimistic-locking

我有以下方法从数据库表中提取数据

    public User getNextUser() {
            final EntityManager em = getEntityManagerFactory().createEntityManager();

            final String sql = "UPDATE user " +
                    "SET processing = TRUE " +
                    "WHERE id = (" +
                                "SELECT id " +
                                "FROM user  " +
                                "WHERE processing = FALSE " +
                                "LIMIT 1 FOR UPDATE) " +
                    "RETURNING *";

            final Query query =  em.createNativeQuery(sql, User.class);
            User result = null;

            try {
                List userList = query.getResultList();
                if (!userList.isEmpty()) {
                    result = (User) userList.get(0);
                }
            } finally {
                em.close();
            }
            return result;
}

如何将此本机查询转换为Hibernate查询(不使第二级缓存无效)?我读过相当于

的内容
  

SELECT ... FOR UPDATE

在Hibernate中,可以通过

实现
  

PESSIMISTIC_WRITE

0 个答案:

没有答案