hibernate中一对多的数据更新问题

lw1373925 2010-01-07 01:12:12
首先定义了2个pojo:
Address class:
public class Address {
private Integer id;
private String address;
private Integer tel;
private User user;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getTel() {
return tel;
}
public void setTel(Integer tel) {
this.tel = tel;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
User class:
import java.util.Set;
public class User {
private Integer id;
private String name;
private Integer age;
private Set<Address> address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Set<Address> getAddress() {
return address;
}
public void setAddress(Set<Address> address) {
this.address = address;
}
}
配置的hbm。xml文件:
User:
<hibernate-mapping package="com.test.pojo">
<class name="User" table="user3">
<id name="id" column="id" type="integer">
<generator class="identity"></generator>
</id>
<property name="name" column="name" type="string"></property>
<property name="age" column="age" type="integer"></property>
<set name="address" cascade="all" fetch="join" order-by="address asc">
<key column="userId"></key>
<one-to-many class="Address"/>
</set>
</class>
</hibernate-mapping>
Address:
<hibernate-mapping package="com.test.pojo">
<class name="Address" table="address">
<id name="id" column="id" type="integer">
<generator class="identity"></generator>
</id>
<property name="address" column="address" type="string"></property>
<property name="tel" column="tel" type="integer"></property>
<many-to-one name="user" column="userId"></many-to-one>
</class>
</hibernate-mapping>

继承TestCase(Jutil3.0):
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import junit.framework.TestCase;
public class BaseCase extends TestCase {
protected Session session;
protected void setUp() throws Exception {
Configuration conf=new Configuration().configure();
SessionFactory sessionFactory=conf.buildSessionFactory();
session=sessionFactory.openSession();
}
protected void tearDown() throws Exception {
if(session!=null){
session.close();
}
}
}
测试类:(只是截取了更新方法)
import java.util.*;
import org.hibernate.Transaction;
import com.test.pojo.Address;
import com.test.pojo.User;
public class TestOtM extends BaseCase {
public static void main(String[] args) {
TestOtM tom = new TestOtM();
// tom.testCreate();
tom.tUpdate();
// tom.tDelete();
// tom.tQuery();
}
public void tUpdate() {

Transaction ts = session.beginTransaction();

User user = (User) session.load(User.class, 2); //获得id为2的对象
user.setName("xhkhj");

for(Iterator it=user.getAddress().iterator();it.hasNext();){
Address a=(Address) it.next();
a.setAddress("1123123");
a.setTel(123123123);
}
user.setAddress(add);
ts.commit();
}
}
问题是,虽然能执行操作 但是如果我的地址表有多个地址,我只想处理其中一条记录,那么要怎样去更新我想要的记录。
...全文
129 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lw1373925 2010-01-08
  • 打赏
  • 举报
回复
你说的有道理。我只是尝试过捕获User对象,还没考虑过去捕获Address对象,谢谢了。
reandyner 2010-01-07
  • 打赏
  • 举报
回复
那你可以只更新那条地址记录不就行了吗
session.beginTransaction();
Address f=(Address)s.load(Address.class, 1);
f.setAddress("shang");
session.saveOrUpdate(f);
session.beginTransaction().commit();
lw1373925 2010-01-07
  • 打赏
  • 举报
回复
没有此属性
lllwwt 2010-01-07
  • 打赏
  • 举报
回复
<one-to-many class="Address"/> 添加属性cascade=“all”试试
<one-to-many class="Address" cascade="all"/>

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧