2

Two Tables Country And State Both have name and id status columns when insert state table update country column status 'YES' On Hibernate

Like SQL Triggers on Hibernate

How can implement Triggers in Hibernate ????

Kayis Rahman
  • 352
  • 3
  • 13
  • possible duplicate of [How to implement triggers in hibernate](http://stackoverflow.com/questions/4486413/how-to-implement-triggers-in-hibernate) – Stanislav Sep 18 '15 at 09:12

2 Answers2

2

You can use JPA listeners for that, something like :

@Entity
@EntityListeners(StateListener.class)
public class State {
}

and your StateListener class, with the annotation @PostPersist

public StateListener {
  @PostPersist
  public void process(State state) {
    // set the status here
  }
}

hibernate doc on this : https://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/listeners.html

Pras
  • 1,068
  • 7
  • 18
  • Can you post country and state code ? Update your post and i'll update my answer accordingly – Pras Sep 19 '15 at 07:14
0

I think there is one-many relation between Country-State right? You can in Country Entity impl a method like addState(State state){this.status=true; this.states.add(state)}

If you defined the cascading, saving country object will lead to the state object also get saved. And the status flag of country object was set to true.

You can also implement removeState(State state) there you check if the list (states) is empty after the removing, set the status flag to false.

Kent
  • 189,393
  • 32
  • 233
  • 301