Marius Pop
Marius Pop

Reputation: 318

Update an entity with Hibernate whose only one field is editable in DB

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]> 

Upvotes: 1

Views: 427

Answers (3)

Bartun
Bartun

Reputation: 598

Maybe you could try to annotate other properties of Entity as @Column(updatable = false)

Upvotes: 0

Pavel Polyakoff
Pavel Polyakoff

Reputation: 323

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?

Upvotes: 0

Guillaume
Guillaume

Reputation: 14671

Try using dynamic update, the generated SQL will only try to update the columns that have been modified

Upvotes: 1

Related Questions