我所有的代码:
我的更新代码为:
[code]
public void updateTeacher(String tid,teacher tea){
teacher t= (teacher)getHibernateTemplate().load(teacher.class,tid);
t.setName(tea.getName());
t.setSex(tea.getSex());
t.setZhiwei(tea.getZhiwei());
getHibernateTemplate().update(t);
}[/code]父类映射文件:[code]
<class name="com.bean.teacher" table="teacher" lazy="true">
<id name="tid" column="tid">
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="zhiwei" column="zhiwei" />
<set name="student" lazy="true" inverse="true" cascade="all">
<key column="tid"/>
<one-to-many class="com.bean.student"/>
</set>
</class>
[/code]
子类映射文件:[code]
<class name="com.bean.student" table="student">
<id name="sid" column="sid" unsaved-value="null" >
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="grade" column="grade" />
<property name="sclass" column="sclass" /> <many-to-one name="teacher" class="com.bean.teacher" column="tid" not-null="true"/>
</class>
[/code]为什么每次更新都是添加了一条新的信息呢?我搜索了一些资料;都是说要这样:
[code]Parent parent = (Parent) session.load(Parent.class, id);
Child child = (Child) session.load(Child.class, childId); child.setParent(parent);
child.setName("sun"); parent.addChild(child);
s.update(parent); s.flush();
tx.commit();
s.close();
[/code]
才能更新,但是现在我的参数只有父类的:tid,没有子类的childId,那我该怎样才能更新呢??????
我的更新代码为:
[code]
public void updateTeacher(String tid,teacher tea){
teacher t= (teacher)getHibernateTemplate().load(teacher.class,tid);
t.setName(tea.getName());
t.setSex(tea.getSex());
t.setZhiwei(tea.getZhiwei());
getHibernateTemplate().update(t);
}[/code]父类映射文件:[code]
<class name="com.bean.teacher" table="teacher" lazy="true">
<id name="tid" column="tid">
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="zhiwei" column="zhiwei" />
<set name="student" lazy="true" inverse="true" cascade="all">
<key column="tid"/>
<one-to-many class="com.bean.student"/>
</set>
</class>
[/code]
子类映射文件:[code]
<class name="com.bean.student" table="student">
<id name="sid" column="sid" unsaved-value="null" >
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="grade" column="grade" />
<property name="sclass" column="sclass" /> <many-to-one name="teacher" class="com.bean.teacher" column="tid" not-null="true"/>
</class>
[/code]为什么每次更新都是添加了一条新的信息呢?我搜索了一些资料;都是说要这样:
[code]Parent parent = (Parent) session.load(Parent.class, id);
Child child = (Child) session.load(Child.class, childId); child.setParent(parent);
child.setName("sun"); parent.addChild(child);
s.update(parent); s.flush();
tx.commit();
s.close();
[/code]
才能更新,但是现在我的参数只有父类的:tid,没有子类的childId,那我该怎样才能更新呢??????
解决方案 »
- 求指点
- /list/?type=1 是什么意思
- 还是关于通用泛型DAO的理解和实现
- 爱好计算机编程的人留意一下(同时,也诚邀请美工人员参与)
- Tomcat的Host配置,有成功的人能否提供范例?
- 关于SPRING事务管理的问题!
- 用myeclipse+struts+hibernate开发遇到的问题
- ApplicationContext.xml里面报的错
- Hibernate为何无法级联写入对象?
- jb6中能不能添加多个ejbCreate方法?
- 后台java.sql.SQLException:表或视图不存在 , 但拷贝到 sql plus 中却可以顺利执行
- 关于eclipse3.0里的tomcat启动的很奇怪问题,请大家帮忙!up有分,在线等!!
inverse="true"就不需要写parent.addChild(child);
再有,你不包装update的话,自然就可以把儿子加进来,要不你就加多个儿子的参数
还有<class name="com.bean.teacher" table="teacher" lazy="true"
dynamic-insert="true" dynamic-update="true" optimistic-lock="version">这样写的话,对象的状态就交给了session管理,不需要load就可以直接更新,也就是说不需要Parent parent = (Parent) session.load(Parent.class, id); Child child = (Child) session.load(Child.class, childId);
set完值后,直接调用session.update(parent)就可以拉
public class teacherDAOhibernate extends HibernateDaoSupport implements teacherDAO{ public teacherDAOhibernate() {
super();
}
public void updateTeacher(String tid,teacher tea){
Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
try {
teacher a=(teacher)session.load(teacher.class,tid);
a.setName(tea.getName());
a.setSex(tea.getSex());
a.setZhiwei(tea.getZhiwei());
session.update(a);
}
catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
}
}现在用session来进行控制,但是始终还是添加一条数据哟;到底我该怎么修改才能实现更新呢》????
Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
Transaction trans = null;
try {
trans = session.beginTransaction();
teacher a=(teacher)session.load(teacher.class,tid);
a.setName(tea.getName());
a.setSex(tea.getSex());
a.setZhiwei(tea.getZhiwei());
session.update(a);
trans.commit();
}
catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
trans.rollback();
}
finally {
session.close();
}
<class name="com.bean.teacher" table="teacher" lazy="true"
dynamic-insert="true" dynamic-update="true" optimistic-lock="version">程序改 成这样:
public class teacherDAOhibernate extends HibernateDaoSupport implements teacherDAO{ public teacherDAOhibernate() {
super();
}
public void updateTeacher(teacher tea){
Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
try {
session.update(tea);
}
catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
}
}但是始终还是添加一条数据哟;到底我该怎么修改才能实现更新呢》????
我现在正发愁得很!!!!!!!!!!!1
[email protected] POPO
34428546 QQ
随便你加哪个,我发给你