Spring+Hibernate困扰了我几天的问题

xhf1234 xhf1234 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
数据库有2张表
family (id,name)
member (id,name)

其中member通过外键familyID形成一对多关系

我的测试类如下:
************************************
package test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import springDAO.*;
public class Test{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
FamilyDAO fDAO = (FamilyDAO)ctx.getBean("FamilyDAOProxy");
MemberDAO mDAO = (MemberDAO)ctx.getBean("MemberDAOProxy");

Family f = fDAO.findById(1);
Member m = new Member();
m.setName("t");
f.getMembers().add(m);
fDAO.attachDirty(f);
}


}

意图是从family表中读取ID=1的family,然后加入一个属性member,然后更新
但是程序报错了,调试发现在 f.getMembers().add(m); 处报异常
出错信息:
f.getMembers().add(m);
f.getMembers().add(m);
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: springDAO.Family.members, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:183)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:165)
at test.Test.main(Test.java:22)

我试过以下代码段运行正确:
Family f = new Family();
f.setName("t");
Member m = new Member();
m.setName("t");
f.getMembers().add(m);
fDAO.save(f);
也就是只能新建不能更新??????????


大虾们帮帮忙啊,已经因为这问题郁闷了几天了。。。。。。。。
我愿意散尽我所有的分求得答案!!!
谢谢,谢谢 55




用的是MyEclipse,自动生成了很多文件
下边是我的配置文件以及POJO

**********************************************************************************
hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">
localhost
</property>
<property name="connection.password">lifebegin</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<mapping resource="springDAO/Family.hbm.xml" />
<mapping resource="springDAO/Member.hbm.xml" />

</session-factory>

</hibernate-configuration>
*****************************************************************************************


映射文件Member.hbm.xml
******************************************************************************************
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="springDAO.Member" table="member" catalog="ttt">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<many-to-one name="family" class="springDAO.Family" update="false" insert="false" fetch="select">
<column name="id" not-null="true" unique="true" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
</class>
</hibernate-mapping>

映射文件Family.hbm.xml
*************************************************************************************

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="springDAO.Family" table="family" catalog="ttt">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
<set name="members" inverse="true" cascade="all">
<key>
<column name="id" not-null="true" unique="true" />
</key>
<one-to-many class="springDAO.Member" />
</set>
</class>
</hibernate-mapping>

下页续
...全文
评论 11
收藏 已收藏
分享
返回顶部
xhf1234
Web 开发 81,108 成员 341,721 内容