hibernate - omiting entries that

时间:2018-08-22 13:49:41

标签: java hibernate filter event-listener

Situation:

  1. I have an entity (A) that links to the list of entities (B),
  2. Entity B can be in locked state
  3. I have a post load listener that throws exception when hibernate read the object of class B that is locked.

Some pseudocode:

@Entity
@Table(name = "table_a")
public class A {
    @OneToMany( mappedBy = "aid", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<B> listOfBs;

    //some code
}

and class B

@Entity
@Table(name = "table_b")
public class B {
    public boolean isLocked();

    //some code
}

and listener

class DontLoadClosedBsListener implements PostLoadEventListener{

    @Override
    public void onPostLoad(PostLoadEvent event) {
        if(event.getEntity instanceof B){
             if(event.getEntity().isLocked()) throw new VeryPeskyException();
        }
    }
}

The issue is that the listener must stay since it's being used all over the project and too many things depends on that.

My question is: is there any way to tell hibernate to ignore/omit linked entries if there was a problem while reading them?

In other words I want to use my DontLoadClosedBsListener as a filter when working with class A. However I want it to throw an exception when someone will try read locked B outside class A.

Any changes for doing that? Any suggestions?

0 个答案:

没有答案