• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate & JPA question

 
Vladimir Vadlja
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

how difficult would it be to implement trigger-like behaviour using Hibernate & JPA?
For example, if data from one column is changed, store whole row in some "history" table. Is this even possible?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you think of setting up database level triggers with setup code or setup SQL scripts, or do you think of triggers / event listeners in Java code, like @PostUpdate and similar?

(Plus: Envers).
 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to audit events that happen to an entity you can use Hibernate envers.

But if you want to react on events that happen inside hibernate you can use hibernate interceptors.
 
Vladimir Vadlja
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hauke Ingmar Schmidt wrote:Do you think of setting up database level triggers with setup code or setup SQL scripts, or do you think of triggers / event listeners in Java code, like @PostUpdate and similar?

(Plus: Envers).


I was thinking about @PostUpdate kinda thing, thanks. But now when you mentioned, what about setting up a database level trigger?

Volodymyr Levytskyi wrote: If you want to audit events that happen to an entity you can use Hibernate envers.


Thanks Volodymyr, this is what I was looking for.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vladimir Vadlja wrote:But now when you mentioned, what about setting up a database level trigger?


Well, you do it in your database, Hibernate has no direct support for it. You can, of course, define the triggers in an import.sql file that automatically will be executed when Hibernate is run in create mode.

Database trigger have advantages and disadvantages over Hibernate interceptors. As a rule of thumb: When you have different applications working on the same database then you should use triggers so they work the same and automatically for everyone. If it is a single database then I would prefer to have the business logic in one place, one language, and one setup process.
 
Sanjana Kapoor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would like to know the difference between first level cache and second level cache in hibernate and which one is most recommended.

Thanks
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjana Kapoor wrote:Would like to know the difference between first level cache and second level cache in hibernate and which one is most recommended.

Thanks


I recommend to open a new thread for this question. The title of this thread is a bit misleading as it is about one specific question and not a general question thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic