Java 使用 Hibernate 更新表时忽略实体 bean 的空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23285520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ignore null values of entity bean while updating the table using Hibernate
提问by Vishrant
Stuck on the same problemIs there any method to ignore null
values when updating the database in Hibernate
?
卡在同样的问题上null
更新数据库时是否有任何方法可以忽略值Hibernate
?
Whenever you call update(); of Session, it will also update the null values found in the object.
每当你调用 update(); Session 中,它还将更新在对象中找到的空值。
Example:
例子:
User user = new User();
user.setUserId(5);
user.setUserName("Maarten");
user.setUserFirstName(null); // but in database this value is not null
session.update( user);
OR
或者
session.saveOrUpdate( user);
The DB will now update the user, but will set the user firstname to null (because it is null in the object).
DB 现在将更新用户,但会将用户名字设置为 null(因为它在对象中为 null)。
Is there any way or method in Hibernate to avoid this (I don't want to fire a select/ update query to set the bean)? that it will ignore the null value?
Hibernate 中是否有任何方法或方法可以避免这种情况(我不想触发选择/更新查询来设置 bean)?它会忽略空值吗?
回答by Gundamaiah
The dynamic-update attribute tells Hibernate whether to include unmodified properties in the SQL UPDATE statement.
dynamic-update 属性告诉 Hibernate 是否在 SQL UPDATE 语句中包含未修改的属性。
回答by jantonio.aguilera
It depends on the abstraction level you wish to control, and the business rules of the object:
这取决于您希望控制的抽象级别,以及对象的业务规则:
- Is the field reallynullable, or are you simply using it as a DTO? perhaps you need an intermediate object to control null values
- What happens with new users (as in the example)? I should ignore the null value, then which value should be persisted, the default one?
- 该字段是否真的可以为空,或者您只是将其用作 DTO?也许您需要一个中间对象来控制空值
- 新用户会发生什么(如示例中所示)?我应该忽略空值,那么应该保留哪个值,默认值?
If, anyway, you define your requisites in a lower level, e.g. "do not include null values in update queries", you can use the following:
无论如何,如果您在较低级别定义了必备条件,例如“不要在更新查询中包含空值”,则可以使用以下内容:
- sql-update annotation here. You can use a custom sql query or procedure to control values
- dynamicUpdate is about changes, not values, so I think it's not really suitable
- Use a EntityListener, and an auxiliary transient field to revert changes if desired.
- Other options: use an interceptoror even create a custom dialect
- sql-update 注释在这里。您可以使用自定义 sql 查询或过程来控制值
- dynamicUpdate 是关于变化,而不是值,所以我认为它不太合适
- 如果需要,使用EntityListener和辅助瞬态字段来恢复更改。
- 其他选项:使用拦截器甚至创建自定义方言
回答by Vishrant
One of the options is, load
the bean using get(object)
method from the session and then set/change the values. But this comes with an overhead of one extracall to the database. Or you can create a custom query if making an extra call is crucial.
选项之一是load
使用get(object)
会话中的 bean方法,然后设置/更改值。但这会带来对数据库进行一次额外调用的开销。或者,如果进行额外调用至关重要,您可以创建自定义查询。
Edit: if bean
is cached then get(object)
will not be a performance hit.
编辑:如果bean
被缓存,则get(object)
不会影响性能。