简体   繁体   English

如何在更新查询中强制Hibernate使用AND?

[英]How to force Hibernate use AND in update query?

I am trying to update a raw by composite primary key by using hibernate. 我正在尝试通过使用休眠更新由复合主键的原始。

Hibernate uses the next style for such updates: Hibernate使用下一种样式进行此类更新:

update mytable set mycolumn=321 where (left_pk, right_pk) = (123, 456);

Is it possible to force hibernate to use the next style?: 是否可以强制休眠使用下一个样式?:

update mytable set mycolumn=321 where left_pk = 123 and right_pk = 456;

Both queries work but with a huge difference (at least in MariaDB). 这两个查询都可以工作,但是差别很大(至少在MariaDB中)。
If we use repeatable read transaction then the first query locks the whole table for updates and the second query locks only the single row for updates. 如果我们使用repeatable read事务,则第一个查询将锁定整个表以进行更新,而第二个查询将仅锁定一行以进行更新。

I would prefer to lock only a single row, so I need to use the second query. 我宁愿只锁定一行,所以我需要使用第二个查询。

You can go for NamedQueries approach in Hibernate, For example: 您可以在Hibernate中使用NamedQueries方法,例如:

 //Create Query 
 @NamedQueries({ @NamedQuery(name = "   YOUR QUERY NAME", 
        query = "from DeptEmployee where department = :department and emp = :emp") })


// set multiple parameters
 query.setParameter("department",department)
     .setParameter("emp", emp)

Try giving this a shot. 尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM