在ssh三大框架整合时,使用hibernate能很好的控制表与表的关系。但配置稍有疏忽,也容易出错,下面代码就是一种。
我在数据库中没有设置外键关系,而是想通过hibernate来控制,有两个表,栏目表(lm)和课程表(kc),他们是多对一的关系。所以我在Lm的pojo类中如下设置
public
class
Lm
implements
java.io.Serializable
{
//栏目id
private int id;
private String name;
private Kc kc;
//顺序
private int location;
//getter和setter方法
}
{
//栏目id
private int id;
private String name;
private Kc kc;
//顺序
private int location;
//getter和setter方法
}
映射文件Lm.hbm.xml如下设置
<
class
catalog
="eteares"
name
="cn.edu.ujn.wsjx.model.Lm"
table
="lm"
>
< id name ="id" type ="java.lang.Integer" >
< column name ="id" />
< generator class ="identity" />
</ id >
< property name ="name" type ="java.lang.String" >
< column length ="20" name ="name" />
</ property >
< many-to-one name ="kc" class ="cn.edu.ujn.wsjx.model.Kc" >
< column name ="kcId" />
</ many-to-one >
< property name ="location" type ="java.lang.Integer" >
< column name ="location" />
</ property >
</ class >
< id name ="id" type ="java.lang.Integer" >
< column name ="id" />
< generator class ="identity" />
</ id >
< property name ="name" type ="java.lang.String" >
< column length ="20" name ="name" />
</ property >
< many-to-one name ="kc" class ="cn.edu.ujn.wsjx.model.Kc" >
< column name ="kcId" />
</ many-to-one >
< property name ="location" type ="java.lang.Integer" >
< column name ="location" />
</ property >
</ class >
结果部署,运行,显示如下错误
Hibernate:
select
lm0_.id as id5_,
lm0_.name as name5_,
lm0_.kcId as kcId5_,
lm0_.location as location5_
from
eteares.lm lm0_
where
lm0_.kcId =?
2010 - 10 - 2 0 : 35 : 48 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java: 372 )
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java: 3121 )
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java: 232 )
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java: 173 )
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java: 87 )
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java: 862 )
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java: 830 )
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java: 266 )
at org.hibernate.type.EntityType.resolve(EntityType.java: 303 )
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java: 116 )
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java: 842 )
at org.hibernate.loader.Loader.doQuery(Loader.java: 717 )
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java: 224 )
at org.hibernate.loader.Loader.doList(Loader.java: 2145 )
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java: 2029 )
at org.hibernate.loader.Loader.list(Loader.java: 2024 )
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java: 375 )
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java: 308 )
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java: 153 )
at org.hibernate.impl.SessionImpl.list(SessionImpl.java: 1106 )
at org.hibernate.impl.QueryImpl.list(QueryImpl.java: 79 )
at org.springframework.orm.hibernate3.HibernateTemplate$ 29 .doInHibernate(HibernateTemplate.java: 849 )
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java: 372 )
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java: 840 )
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java: 836 )
at cn.edu.ujn.wsjx.dao.impl.LmDao.findByCourse(LmDao.java: 74 )
at cn.edu.ujn.wsjx.managecolumn.service.impl.ColumnService.getColumnByCourse(ColumnService.java: 56 )
at cn.edu.ujn.wsjx.managecolumn.action.ColumnAction.browseColumn(ColumnAction.java: 59 )
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
select
lm0_.id as id5_,
lm0_.name as name5_,
lm0_.kcId as kcId5_,
lm0_.location as location5_
from
eteares.lm lm0_
where
lm0_.kcId =?
2010 - 10 - 2 0 : 35 : 48 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java: 372 )
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java: 3121 )
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java: 232 )
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java: 173 )
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java: 87 )
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java: 862 )
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java: 830 )
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java: 266 )
at org.hibernate.type.EntityType.resolve(EntityType.java: 303 )
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java: 116 )
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java: 842 )
at org.hibernate.loader.Loader.doQuery(Loader.java: 717 )
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java: 224 )
at org.hibernate.loader.Loader.doList(Loader.java: 2145 )
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java: 2029 )
at org.hibernate.loader.Loader.list(Loader.java: 2024 )
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java: 375 )
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java: 308 )
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java: 153 )
at org.hibernate.impl.SessionImpl.list(SessionImpl.java: 1106 )
at org.hibernate.impl.QueryImpl.list(QueryImpl.java: 79 )
at org.springframework.orm.hibernate3.HibernateTemplate$ 29 .doInHibernate(HibernateTemplate.java: 849 )
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java: 372 )
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java: 840 )
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java: 836 )
at cn.edu.ujn.wsjx.dao.impl.LmDao.findByCourse(LmDao.java: 74 )
at cn.edu.ujn.wsjx.managecolumn.service.impl.ColumnService.getColumnByCourse(ColumnService.java: 56 )
at cn.edu.ujn.wsjx.managecolumn.action.ColumnAction.browseColumn(ColumnAction.java: 59 )
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
经诊断,此为hibernate的设置问题,因为<many-to-one>默认的lazy属性为true,即使用了懒加载,而懒加载是在用到该对象的属性时才会加载数据库。所以应该把在标签中加入lazy="false"