Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 在ehcache无法正常工作的情况下休眠_Spring_Hibernate_Ehcache - Fatal编程技术网

Spring 在ehcache无法正常工作的情况下休眠

Spring 在ehcache无法正常工作的情况下休眠,spring,hibernate,ehcache,Spring,Hibernate,Ehcache,我的项目使用Spring4和Hibernate4。 最近,我在项目中添加了ehcache。有一个实体com.xxx.Employee持有另一个实体com.xxx.User。我使用HibernateTemplate findByCriteria(DetachedCriteria,int,int)执行查询,然后,通过HibernateTemplate get(entityName,id)进行另一个查询,日志中没有显示SQL,这似乎第二个缓存工作,但是,Employee的用户缺失,没有引发“org.h

我的项目使用Spring4和Hibernate4。 最近,我在项目中添加了ehcache。有一个实体com.xxx.Employee持有另一个实体com.xxx.User。我使用HibernateTemplate findByCriteria(DetachedCriteria,int,int)执行查询,然后,通过HibernateTemplate get(entityName,id)进行另一个查询,日志中没有显示SQL,这似乎第二个缓存工作,但是,Employee的用户缺失,没有引发“org.hibernate.LazyInitializationException:无法初始化代理-无会话”

是否需要为将对象的对象值保存到缓存添加其他设置

<hibernate-mapping package="com.xxx">
<class name="Employee" table="t_employee">
    <cache usage="read-write" include="all"/>
    <id name="personId" column="person_id">
        <generator class="identity" />
    </id>

    <property name="name" column="name" />

    <many-to-one name="department" column="department_id" fetch="join" not-null="false" />
    <many-to-one name="user" column="user_id" fetch="join" not-null="false" cascade="all-delete-orphan" unique="true"/>

</class>


public T getOneById(可序列化的id)引发TamsException{
if(id==null){
logger.error(“getOneById为null”);
抛出新的TamsException(“getOneById为null”);
}
T;
试一试{
log.info(“get”+getEntityName()+”通过ID:“+ID”);
HibernateTemplate ht=getHibernateTemplate();
ht.setCacheQueries(true);
t=(t)ht.get(getEntityName(),id);
返回t;
}捕获(例外e){
抛出新的TamsException(“get”+getEntityName()+“:failure!”,e);
}最后{
log.info(“通过ID:+ID+“success!”)获取“+getEntityName()+”;
}
}
公共列表查询条件(查询条件条件)
性感觉{
log.info(“查询”+getEntityName()+”由“+condition+”…”);
试一试{
int firstResult=0;
int maxResults=0;
DetachedCriteria c=DetachedCriteria
.forEntityName(getEntityName());
if(条件!=null){
firstResult=condition.getFirstResult();
maxResults=condition.getMaxResults();
装配体(条件,c);
}
HibernateTemplate ht=getHibernateTemplate();
ht.setCacheQueries(true);
@抑制警告(“未选中”)
List=(List)ht.findByCriteria(c,firstResult,
最大结果);
log.info(“查询”+getEntityName()+”由:“+条件
+“ret size-->”+list.size());
退货清单;
}捕获(例外e){
通过以下方式引发新的TamsException(“查询”+getEntityName()+”:
+条件+“故障!”,e);
}
}

您收到
org.hibernate.LazyInitializationException:无法初始化代理-无会话
错误,因为属性
Employee.user
配置了选项
lazy=proxy
(未提及时,这是默认设置)

这是:

惰性(可选-默认为代理):默认情况下,单点 关联是代理的。lazy=“no proxy”指定属性 应该在第一次访问实例变量时延迟获取。 这需要编译时字节码插装。lazy=“false” 指定总是急切地获取关联

因此,现在您有两个选择:

  • 使用
    lazy=false
    急切地获取关联
  • 使用所需关联之前,请打开
    会话

  • 如果我不使用缓存,代码工作得很好,所以我认为问题是当Hibernate从缓存中获取对象时,它会丢失一些东西。最后,我使用Spring缓存而不是Hibernate二级缓存。
    <hibernate-mapping package="com.xxx">
    <class name="User" table="t_user">
        <cache usage="read-write"/>
        <id name="userId" column="user_id">
            <generator class="identity" />
        </id>
        <property name="name" column="name" not-null="true" />
        <property name="loginName" column="login_name" unique="true" not-null="true" />
        <property name="password" column="password" not-null="true"  />
        <many-to-one name="employee" column="employee_id" fetch="join" not-null="false" />
        <many-to-one name="role" class="Role" column="role_id" fetch="join" not-null="true" />
        <property name="lock" column="col_lock" type="integer" />
        <property name="createDate" column="create_date" />
        <property name="updateDate" column="update_date" />
        <property name="passwordUpdateDate" column="pwd_update_date" />
        <property name="loginTimes" column="login_times" type="integer" />
        <property name="lastLoginDate" column="last_login_date" />
        <property name="comment" column="comment" />
    
    </class>
    
    public T getOneById(Serializable id) throws TamsException {
        if (id == null) {
            logger.error("getOneById id is null");
            throw new TamsException("getOneById id is null");
        }
        T t;
        try {
            log.info("get " + getEntityName() + " by ID : " + id);
    
            HibernateTemplate ht = getHibernateTemplate();
            ht.setCacheQueries(true);
            t = (T) ht.get(getEntityName(), id);
            return t;
        } catch (Exception e) {
            throw new TamsException("get " + getEntityName() + ":  failure!", e);
        } finally {
            log.info("get " + getEntityName() + " by ID : " + id + " success!");
        }
    }
    
    
    public List<T> queryByCondition(QueryCondition condition)
            throws TamsException {
    
        log.info("query " + getEntityName() + " by: " + condition + " ...");
        try {
            int firstResult = 0;
            int maxResults = 0;
    
            DetachedCriteria c = DetachedCriteria
                    .forEntityName(getEntityName());
    
            if (condition != null) {
                firstResult = condition.getFirstResult();
                maxResults = condition.getMaxResults();
                assembleCriteria(condition, c);
            }
            HibernateTemplate ht = getHibernateTemplate();
            ht.setCacheQueries(true);
            @SuppressWarnings("unchecked")
            List<T> list = (List<T>) ht.findByCriteria(c, firstResult,
                    maxResults);
    
            log.info("query " + getEntityName() + " by: " + condition
                    + " ret size --> " + list.size());
            return list;
        } catch (Exception e) {
            throw new TamsException("query " + getEntityName() + " by: "
                    + condition + " failure!", e);
        }
    
    }