Search code examples
javahibernatecriteriahibernate-criteriamany-to-one

Java Hibernate temporarily Ignore @ManyToOne annotaion with criteria


I have a Hibernate query with Criteria.

What I would like to do is (just for one query) tell hibernate to ignore an existing @ManyToOne annotation.

that is because hibernate creates and Left join on other tables. I could figure it how to do it.

I have found this 2 links which didn't solve my problem:

Hibernate: How to remove an entity to which none refers to anymore in ManyToOne?

What is the difference between DELETE_ORPHAN and DELETE?


Solution

  • If you have such mapping:

    //Parent
    public class A {
        ...
    }
    
    //Child
    public class B {
       private A parent; //Many to one
       ...
    }
    

    Please try something like this:

    Criteria q = ....;
    q.setFetchMode("parent", FetchMode.SELECT);
    ....