java @javax.persistence.Column( 可更新=假)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11993274/
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
@javax.persistence.Column( updatable=false )
提问by Mike Baranczak
If I define a field as non-updatable, does that mean that the field can't be updated at all, ever? What I'd like to do is prevent the field from getting updated when I save an entire entity object, but still be able to write queries that explicitly update this field. Is this possible?
如果我将一个字段定义为不可更新,是否意味着该字段根本无法更新?我想要做的是在我保存整个实体对象时防止该字段被更新,但仍然能够编写显式更新该字段的查询。这可能吗?
I'm using Hibernate and MySQL, but I'd prefer to write portable code.
我正在使用 Hibernate 和 MySQL,但我更喜欢编写可移植的代码。
回答by Mikko Maunu
As documented, setting updatable=false
affects only SQL UPDATE statements generatedby persistence provider:
如文档所述,设置updatable=false
仅影响持久性提供程序生成的SQL UPDATE 语句:
Whether the column is included in SQL UPDATE statements generated by the persistence provider.
该列是否包含在持久性提供程序生成的 SQL UPDATE 语句中。
You are still able to write such an SQL update statement that will update values in this column. This functionality defined in JPA specification, and is not Hibernate/MySQL specific.
您仍然可以编写这样的 SQL 更新语句来更新此列中的值。此功能在 JPA 规范中定义,并非特定于 Hibernate/MySQL。
回答by Bharat Sinha
@javax.persistence.Column( updatable=false )
The column mapped would not be updatable using HQL or via hibernate methods. In case you need to update the value in the DB you should be writing native SQL.
映射的列不能使用 HQL 或通过休眠方法更新。如果您需要更新数据库中的值,您应该编写本机 SQL。
And if I see logically, it shouldn't allow you to update when you say updatable=false
如果我从逻辑上看,它不应该让你在你说的时候更新 updatable=false