Ignore Hibernate @Where annotation

后端 未结 5 1786
孤独总比滥情好
孤独总比滥情好 2020-12-17 10:05

I have an Entity which has an association to another Entity annotated with @Where, like so

public class EntityA {

    @OneToMany
    @Where(...)
    private         


        
相关标签:
5条回答
  • 2020-12-17 10:26

    After lots of research it appears that this is simply impossible. I'd strongly suggest avoiding @Where, as it is hard to predict beforehand if you'll ever need those associations or not.

    0 讨论(0)
  • 2020-12-17 10:28

    I know its an old question but in case anyone look for an answer...
    I had the same dilemma, you don't want to mess your code with "is_deleted= false" every where you select,

    simple solution is to use native SQL:

    lst = sessionFactory.getCurrentSession().
    createSQLQuery("select {entb.*}  from EntityB entb where  is_deleted=1")                            
               .addEntity("entb", EntityB.class)
               .list();
    
    0 讨论(0)
  • 2020-12-17 10:38

    I just had the same question. I solved the problem like this:

    @Query(value="SELECT * FROM EntityA", nativeQuery=true)
    public ignoreWhereMethod(){}
    

    The SQL in the @Query annotation must point the table's name and the fields' names (not entity's name).

    0 讨论(0)
  • 2020-12-17 10:40

    You can map another property with same data like this:

    public class EntityA {
    
        @OneToMany
        @JoinColumn(name='theColumnName', insertable=false, updateable=false)
        private List<EntityB> entityBListReadOnly;
    
    }
    

    Important to set as updateable false and insertable falso to avoid consistency problems in your data!

    0 讨论(0)
  • 2020-12-17 10:43

    Another solution is filter, as explained in this question. Hibernate how to ignore @Where annotation

    I will be trying this myself because the combination of @where and @NamedNativeQueries is a very cumbersome solution in our situation. But I do not know yet if @Filter is any better.

    0 讨论(0)
提交回复
热议问题