Ignore Hibernate @Where annotation

跟風遠走 提交于 2019-12-01 13:54:09

问题


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

public class EntityA {

    @OneToMany
    @Where(...)
    private List<EntityB> entityBList;

}

Recently the inevitable has happened, I need to load EntityB's that don't conform to the @Where clause. I could remove the @Where annotation, but it is used a lot, so ideally I don't want to do that. Apart from loading the list of EntityB's manually, with another query, what are my options? Can I tell Hibernate to ignore the @Where annotation?


回答1:


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.




回答2:


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




回答3:


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



回答4:


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!



来源:https://stackoverflow.com/questions/2311125/ignore-hibernate-where-annotation

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