WelcomeTo
WelcomeTo

Reputation: 20591

What is usually way for updating entities in Hibernate/JPA?

What approach is more preferable when updating entity in Hibernate? (new values comes from UI)

1) Retrive entity by ID (Session#get()), update it fileds with new values and call Session#update().

2) Create new entity using new keyword, populate it with new values, insert ID (all values including ID received from UI) and call Session#update().

Upvotes: 0

Views: 869

Answers (1)

Stefan Steinegger
Stefan Steinegger

Reputation: 64648

Get entities from the database using session.get or queries, change it, commit the session.

Hibernate implements a very useful auto flush mechanism which detects changes in the session and synchronizes them automatically to the database.

Upvotes: 1

Related Questions