hibernate session get返回null

时间:2012-03-08 21:05:10

标签: hibernate session get

我的hibernate查询返回null。我在互联网上搜索但是找不到这个具体问题的答案。这是查询:

public T findById(Long id){
    return (T) sessionFactory.getCurrentSession().get(getPersistentClass(), id);
}

具有id的实体存在于数据库中,但此查询始终返回null,我不知道原因。这是生成的查询:

Hibernate: select quiz0_.QUIZ_ID as QUIZ1_3_0_, quiz0_.CATEGORY as CATEGORY3_0_, quiz0_.CREATED_DATE as CREATED3_3_0_, quiz0_.CREATOR_ID as CREATOR4_3_0_, quiz0_.DESCRIPTION as DESCRIPT5_3_0_, quiz0_.IS_IMMEDIATE_CORRECTION as IS6_3_0_, quiz0_.IS_MULTIPAGE as IS7_3_0_, quiz0_.NUM_OF_QUESTIONS as NUM8_3_0_, quiz0_.IS_PRACTICE_MODE as IS9_3_0_, quiz0_.TITLE as TITLE3_0_, quiz0_.IS_RANDOM as IS11_3_0_, quiz0_.RATING as RATING3_0_ from QUIZ quiz0_ where quiz0_.QUIZ_ID=?

提前致谢。

2 个答案:

答案 0 :(得分:1)

我已经完成以下操作以确保在扩展和使用此dao实现时设置类(不是完整的dao实现)

public abstract class GenericDaoImpl<T, I extends Serializable> implements GenericDao<T, I>{
        private Class<T> type;
        public GenericDaoImpl() {
            this.type = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        }
        public T find(I id) {
            Assert.notNull(id);
            return (T) getSessionFactory().getCurrentSession().get(getType(), id);
        }
}

答案 1 :(得分:0)

如果您的数据库表QUIZ映射到T

尝试T.class而不是getPersistentClass()

或者您可以使用实体名称

更改T.class