更新一个在Hibernate中,数据库中只有一个字段是可编辑的实体。

huangapple go评论84阅读模式
英文:

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

&lt;2020-09-17 14:33:26.735  WARN 26939 --- [ (self-tuning)&#39;] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1031, SQLState: 42000&gt; 
&lt;2020-09-17 14:33:26.735 ERROR 26939 --- [ (self-tuning)&#39;] o.h.engine.jdbc.spi.SqlExceptionHelper   : ORA-01031: insufficient privileges&gt; 
&lt;2020-09-17 14:33:26.736  INFO 26939 --- [ (self-tuning)&#39;] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements&gt; 
&lt;2020-09-17 14:33:26.737 ERROR 26939 --- [ (self-tuning)&#39;] o.h.i.ExceptionMapperStandardImpl        : HHH000346: Error during managed flush [org.hibernate.exception.SQLGrammarException: could not execute statement]&gt; 


</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&#39;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>



huangapple
  • 本文由 发表于 2020年9月17日 19:48:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63937393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定