Ignore Hibernate @Where annotation

99封情书 提交于 2019-12-01 15:03:40

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.

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).

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();

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!