18

I want to implement something similar to triggers in hibernate.

What I need is when a column in a table attains a specific value, a row should be inserted in another table or some other table should be updated.

How do I implement this in hibernate ?

n00begon
  • 3,503
  • 3
  • 29
  • 42
Juzer Arsiwala
  • 601
  • 2
  • 10
  • 20

1 Answers1

26

That's described in Chapter 14. Interceptors and events.

You can, for example, intercept a post-update event.

However, using the event system for business operations might not be the best idea (they are best used for infrastructure concerns). You should use a higher level layer for that.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • 2
    hey could you please elaborate why using event system in this case might not be a good idea.. is there a problem which i am not seeing ?? – Juzer Arsiwala Dec 20 '10 at 02:52
  • 6
    @Juzer it's just considered good practice. Your data layer should be simple and dumb, while your service layer should do most of the work. And creating new data in response to an event is something that belongs in the service layer, not the data layer (even though it's probably easier to achieve in the data layer). – Sean Patrick Floyd Dec 20 '10 at 08:19
  • What about time stamps like [I'm asking here](http://stackoverflow.com/questions/30544951/propagate-last-updated-time-stamp-from-sub-tables-to-main-table)? Would one use the event system or interceptors? – Stefan Falk May 30 '15 at 10:37
  • Is there one way to intercept or listen to list of saveAll(List..) method? – Jonathan JOhx Dec 17 '18 at 19:11
  • 1
    @JonathanJohx I recommend asking that as a separate question. This is an 8 year-old answer and I haven't used NHibernate for over 5 years. – Diego Mijelshon Dec 17 '18 at 23:21
  • @DiegoMijelshon It's OK, thank you! I hope you can answer this question https://stackoverflow.com/questions/53824506/how-to-implement-triggers-to-intercept-a-collection-in-hibernate , thanks a lot in advance. – Jonathan JOhx Dec 17 '18 at 23:40