首页 新闻 搜索 专区 学院

如何解决ssh框架一对多延迟加载的问题

0
悬赏园豆:20 [待解决问题]

项目是一个ssh框架项目,在配置延迟加载时一直配不对

 

recUser.hbm.xml如下(one的一方)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-12-28 15:14:31 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.cpsec.seventang.lawyerRecruitment.entity.RecUser" table="rec_user" catalog="casserver">
        <id name="userId" type="java.lang.Integer">
            <column name="user_id" />
            <generator class="identity" />
        </id>
        
        <property name="company_id" type="java.lang.Integer">
            <column name="company_id"></column>
        </property>
        
        <property name="userImg" type="string">
            <column name="user_img">
            </column>
        </property>
        <property name="userName" type="string">
            <column name="user_name">
            </column>
        </property>
        <property name="userPassword" type="string">
            <column name="user_password" length="20" />
        </property>
       
        <set name="recEducations" table="rec_education" inverse="true" lazy="true" >
            <key>
                <column name="user_id" />
            </key>
            <one-to-many class="com.cpsec.seventang.lawyerRecruitment.entity.RecEducation" />
        </set>
        
    </class>
</hibernate-mapping>

 

 

RecEducation.hbm.xml(many的一方)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-12-28 15:14:31 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.cpsec.seventang.lawyerRecruitment.entity.RecEducation" table="rec_education" catalog="casserver">
        <id name="eduId" type="java.lang.Integer">
            <column name="edu_id" />
            <generator class="identity" />
        </id>
        <many-to-one name="recUser" class="com.cpsec.seventang.lawyerRecruitment.entity.RecUser" fetch="join" lazy="false">
            <column name="user_id" />
        </many-to-one>
        <property name="eduSchool" type="string">
            <column name="edu_school" length="30">
            </column>
        </property>
        <property name="eduMajor" type="string">
            <column name="edu_major" length="30">
            </column>
        </property>
        <property name="eduDegree" type="string">
            <column name="edu_degree" length="30">
            </column>
        </property>
        <property name="eduStart" type="string">
            <column name="edu_start" length="8">
            </column>
        </property>
        <property name="eduEnd" type="string">
            <column name="edu_end" length="8">
            </column>
        </property>
        <property name="eduExperience" type="string">
            <column name="edu_experience">
            </column>
        </property>
    </class>
</hibernate-mapping>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    <display-name>lawyer</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <filter>
        <filter-name>OpenSessionInView</filter-name>
           <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
          <init-param>
               <param-name>singleSession</param-name>
               <param-value>true</param-value>
          </init-param>
     </filter>
     <filter-mapping>
           <filter-name>OpenSessionInView</filter-name>
          <url-pattern>/*</url-pattern>
     </filter-mapping>
    
    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
</web-app>
  

 

package lawyerRecruitment;


import java.util.Map;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.cpsec.seventang.lawyerRecruitment.biz.UserBiz;
import com.cpsec.seventang.lawyerRecruitment.dao.UserDao;
import com.cpsec.seventang.lawyerRecruitment.entity.RecUser;
import com.cpsec.seventang.lawyerRecruitment.util.ParserToMap;

import net.sf.json.JSONObject;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class UserTest {
    @Resource
    private UserDao userDao;
    
    @Resource
    private UserBiz userBiz;
    
    @Test
    public void userToJson(){
        RecUser user=userDao.queryRecUser("18301221126");
        System.out.println(user);//可以打印出来
        System.out.println(user.getRecEducations().size());//运行到这一行程序停止并且不报错,junit打印异常
        System.out.println("ggggggggg");
    }

}

 

异常信息

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.cpsec.seventang.lawyerRecruitment.entity.RecUser.recEducations, no session or session was closed
	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
	at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122)
	at org.hibernate.collection.PersistentSet.size(PersistentSet.java:162)
	at lawyerRecruitment.UserTest.userToJson(UserTest.java:34)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

有没有大神指导一下

布诺的主页 布诺 | 初学一级 | 园豆:182
提问于:2017-01-01 15:50
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册