One-root-entity-per-returned-row phenomenon of hibernate

来源:互联网 发布:java怎么编写分段函数 编辑:程序博客网 时间:2024/06/05 20:38

 

public static List t() {
        List r 
= new ArrayList();
        Session s 
= Sf.getSession();
        Transaction tx;
        tx 
= s.beginTransaction();
        String sql;

        
try {
            r 
= s.createCriteria(JtsUser.class)
                 .add(Restrictions.idEq(
2))
                 .setFetchMode(
"jtsHistories", FetchMode.JOIN)
                 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                 .list();
            tx.commit();
        }
 catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        }
 finally {
            s.close();
        }

        
return r;
    }

    

    
public static List tt() {
        List r 
= new ArrayList();
        Session s 
= Sf.getSession();
        Transaction tx;
        tx 
= s.beginTransaction();
        String sql;

        
try {
            r 
= s.createCriteria(JtsUser.class)
                 .add(Restrictions.idEq(
2))
                 .setFetchMode(
"jtsHistories", FetchMode.JOIN)
                 .list();
            tx.commit();
        }
 catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        }
 finally {
            s.close();
        }

        
return r;
    }

t().size() is 1; While tt().size() is 4, that's the one-root-entity-per-returned-row phenomenon of hibernate.

Add "setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)" to eliminate the one-root-entity-per-returned-row phenomenon that you're seeing. 

原创粉丝点击