67,512
社区成员
发帖
与我相关
我的任务
分享
<hibernate-mapping>
<class name="org.project.oa.entity.UserInfo" table="UserInfo" schema="dbo" catalog="oaDB">
..............
<many-to-one name="purviewInfo" class="org.project.oa.entity.PurviewInfo" fetch="select" >
<column name="purviewId" not-null="true" />
</many-to-one>
................
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="org.project.oa.entity.PurviewInfo" table="PurviewInfo" schema="dbo" catalog="oaDB">
.........................
<set name="userInfos" inverse="true">
<key>
<column name="purviewId" not-null="true" />
</key>
<one-to-many class="org.project.oa.entity.UserInfo" />
</set>
..........................
</class>
</hibernate-mapping>
public List search(int userId){
UserInfo userInfo=userInfoDao.get(userId);//正常加载
PurviewInfo purviewInfo=userInfo.getPurviewInfo();//出现延迟加载异常
return functionDao.search(purviewInfo.getPurviewStr());
}
public PurviewInfo getByUserId(int userId) {
return (PurviewInfo)super.getHibernateTemplate().execute(
new HibernateCallback(){
public Object doInHibernate(Session s){
String hql=" from UserInfo u join fetch u.purviewInfo where u.id="+userId;
return ((UserInfo)(s.createQuery(hql).uniqueResult())).getPurviewInfo();
}
}
);
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- data source -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
<!-- dao层 -->
<bean id="daoBase" class="org.project.oa.dao.impl.DaoBase">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="functionDao" class="org.project.oa.dao.impl.FunctionDaoImpl" parent="daoBase"/>
<bean id="userInfoDao" class="org.project.oa.dao.impl.UserInfoDaoImpl" parent="daoBase"/>
<bean id="purviewInfoDao" class="org.project.oa.dao.impl.PurviewInfoDaoImpl" parent="daoBase"/>
<!-- biz层 -->
<bean id="functionBiz" class="org.project.oa.biz.impl.FunctionBizImpl">
<property name="functionDao" ref="functionDao"/>
<property name="purviewInfoDao" ref="purviewInfoDao"></property>
<property name="userInfoDao" ref="userInfoDao"></property>
</bean>
<!-- action层 -->
<bean name="/treeView" class="org.project.oa.web.action.TreeViewAction">
<property name="functionBiz" ref="functionBiz"></property>
</bean>
</beans>
public class FunctionBizImpl implements FunctionBiz{
private FunctionDao functionDao=null;
private PurviewInfoDao purviewInfoDao=null;
private UserInfoDao userInfoDao=null;
public List search(int userId){
UserInfo userInfo=userInfoDao.get(userId);//正常获取
PurviewInfo purviewInfo=userInfo.getPurviewInfo();//出现延迟加载异常
return functionDao.search(purviewInfo.getPurviewStr());
}
//getter&setter
...................
public class UserInfoDaoImpl extends DaoBase implements UserInfoDao{
/**
* 通过id获取用户信息
* @param id
* @return
*/
public UserInfo get(int id) {
return (UserInfo)super.get(UserInfo.class, id);
}
}
DaoBase类