延迟加载在其他方法上失败

时间:2013-07-03 22:09:02

标签: hibernate

我有一个父/子集,使用session.get(Parent.class,index)时我没有遇到任何问题。 Parent类具有我想要使用的varchar而不是索引ex:ldapname。问题是我还没有找到其他有效的方法。我已经尝试了session.createSQLQuery,只是createQuery,它们以某种方式失败了。

我有什么......

@Entity
@Table(name="CAVERE_USER")
public class CavereUser {

    @Id @GeneratedValue
    @Column(name = "user_id")
    private int user_id;

    @Column(name="first_name", nullable=false, columnDefinition="varchar", length=20, insertable=true, updatable=true)
    private String first_name;

    @Column(name="last_name", nullable=false, columnDefinition="varchar", length=20, insertable=true, updatable=true)
    private String last_name;

    @OneToMany
    @JoinColumn(name="user_id", insertable=true, updatable=true)
    @org.hibernate.annotations.IndexColumn(name="USER_ID")
    private Set<CavereAttr> cuattr = new HashSet<CavereAttr>();

getter & setters

儿童

@Entity
@Table(name="Cavere_Attr")
public class CavereAttr {

    @Id @GeneratedValue
    @Column(name = "ATTR_ID")
    private int attr_id;

    @Column(name="privlege", nullable=false, columnDefinition="varchar", length=20, insertable=true, updatable=true)
    private String privlege;

    @Column(name = "USER_ID")
    private Long user_id;
getters & setters

DAO

    @Autowired
    @Qualifier("cavereSessionFactory")
    private SessionFactory sessionFactory;

    @Override
    public CavereUser getCavereUserById(int user_id) {
        Session session = null;
        CavereUser cavereuser = null;
        try
        {
            session = sessionFactory.openSession();
            session.beginTransaction();
            cavereuser = (CavereUser)session.get(CavereUser.class, user_id);
            System.out.println("The Attr " + cavereuser.getCuattr().size());
            session.getTransaction().commit();
        }
        catch (RuntimeException e)
        {           
            System.out.println (e.getMessage());
        }   
        finally
        {
            if (session != null)
            {           
                session.close();
            }   
        }       
        return cavereuser;
    }

    @Override
    public CavereUser getCavereUserByLastName(String lastname){
        Session session = null;
        CavereUser cavereuser = null;

        int user_id = 0;
        String struser_id;
        try
        {
            session = sessionFactory.openSession();
            session.beginTransaction();

/*
 * Using JDK 7 with Spring 3 and Hibernate 4            
 */
/*
 * creatSQLQuery is returning Query for setParameter() instead of SQLQuery          
 */

            // This works getting the Parent & Child classes    
            SQLQuery sqlq = session.createSQLQuery("select user_id from cavere_user where last_name = '" + lastname + "'");

            user_id = (Integer)sqlq.uniqueResult();
            cavereuser = getCavereUserById(user_id);


            /*
             * the code below can't get the childs
             * failed to lazily initialize a collection of role: com.h1mny.implementation.CavereUser.cuattr, 
             * no session or session was closed
             */

/*          
            List<CavereUser> sUserList = session.createCriteria(CavereUser.class)
                .add(Restrictions.eq("last_name", "Lopes".toLowerCase()).ignoreCase())
*/              
/*
 * Adding this causes a syntax error that the select has the wrong syntaxt for MYSQL
                .setMaxResults(1)
*/
/*          
                .list();
            if(sUserList != null && sUserList.size() == 1){
                cavereuser = sUserList.get(0);
            }
 */         
            session.getTransaction().commit();
        }
        catch (RuntimeException e)
        {           
            System.out.println (e.getMessage());
        }   
        finally
        {
            if (session != null)
            {           
                session.close();
            }   
        }       
        return cavereuser;

    }
}

1 个答案:

答案 0 :(得分:0)

尝试更改加入此

@OneToMany
@JoinColumn(name="user_id", insertable=true, updatable=true, fetch = FetchType.EAGER)
@org.hibernate.annotations.IndexColumn(name="USER_ID")
private Set<CavereAttr> cuattr = new HashSet<CavereAttr>();

我希望这能解决你的问题