英文:
Update an entity with Hibernate whose only one field is editable in DB
问题
我有一个实体类 Product,我被赋予了对整个实体的读取授权,并且只有在数据库中的 price 字段上有更新授权。因此,无论何时我调用 productDao.update(product),即使我只更改了价格字段的值,我都会收到以下警告和错误:
<2020-09-17 14:33:26.735 警告 26939 --- [ (self-tuning)''] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL 错误: 1031, SQLState: 42000>
<2020-09-17 14:33:26.735 错误 26939 --- [ (self-tuning)''] o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-01031: 权限不足>
<2020-09-17 14:33:26.736 信息 26939 --- [ (self-tuning)''] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: 在批处理释放时仍包含 JDBC 语句>
<2020-09-17 14:33:26.737 错误 26939 --- [ (self-tuning)''] o.h.i.ExceptionMapperStandardImpl : HHH000346: 托管刷新期间出错 [org.hibernate.exception.SQLGrammarException: 无法执行语句]>
英文:
I have an entity class Product that i was given READ grant on the whole entity and UPDATE grant ONLY on the price field in the database. So whenever i call productDao.update(product), even if I changed only the price field value, i`m getting
<2020-09-17 14:33:26.735 WARN 26939 --- [ (self-tuning)'] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1031, SQLState: 42000>
<2020-09-17 14:33:26.735 ERROR 26939 --- [ (self-tuning)'] o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-01031: insufficient privileges>
<2020-09-17 14:33:26.736 INFO 26939 --- [ (self-tuning)'] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements>
<2020-09-17 14:33:26.737 ERROR 26939 --- [ (self-tuning)'] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [org.hibernate.exception.SQLGrammarException: could not execute statement]>
</details>
# 答案1
**得分**: 1
尝试使用[动态更新(dynamic update)][1],生成的SQL仅会尝试更新已被修改的列。
<details>
<summary>英文:</summary>
Try using [dynamic update][1], the generated SQL will only try to update the columns that have been modified
[1]: https://www.baeldung.com/spring-data-jpa-dynamicupdate
</details>
# 答案2
**得分**: 0
或许可以不调用默认的 .update() 方法,而是实现自己的更新方法,在该方法中只执行一个基于对象值的 HQL 更新语句,来更新一个字段的值?
<details>
<summary>英文:</summary>
Maybe instead of calling the default .update() method implement your own updating method in which you just execute a HQL update statement which updates one field based on your object's value?
</details>
# 答案3
**得分**: 0
也许你可以尝试对Entity的其他属性进行注释,例如@Column(updatable = false)。
<details>
<summary>英文:</summary>
Maybe you could try to annotate other properties of Entity as @Column(updatable = false)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论