Posted on 2007-01-08 16:55
帅子 阅读(899)
评论(1) 编辑 收藏 所属分类:
j2se技术专区
二.双向一对一关联
需要对TGroup.java和TGroup.hbm.xml文件作修改
1.在TGroup.java中增加一个TUser类和相应的getter、setter方法
TGroup.java
package cn.blogjava.start;
import java.io.Serializable;
public class TGroup implements Serializable {
private Integer id;
private String name;
private TUser user;
public TUser getUser() {
return user;
}
public void setUser(TUser user) {
this.user = user;
}
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;
}
}
2.在TGroup.hbm.xml中增加one-to-one配置
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.blogjava.start.TGroup" table="T_Group" catalog="sample">
<id name="id" type="integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="string" column="name" />
<one-to-one
name="user"
class="cn.blogjava.start.TUser"
property-ref="group"
/>
</class>
</hibernate-mapping>
</DIV>
这样就实现了双向关联。可以实现双向查询
package cn.blogjava.start;
import java.util.List;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest extends TestCase {
Session session = null;
protected void setUp() {
try {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
} catch (HibernateException e) {
e.printStackTrace();
}
}
protected void tearDown() {
try {
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
/**
* 对象持久化测试(Insert方法)
*/
public void testInsert() {
Transaction tran = null;
try {
TUser user = new TUser();
user.setName("byf");
user.setAge(new Integer(26));
TGroup group = new TGroup();
group.setName("Admin");
tran = session.beginTransaction();
//设置关联
user.setGroup(group);
session.save(user);
session.flush();
tran.commit();
Assert.assertEquals(user.getId().intValue()>0 ,true);
} catch (HibernateException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
if(tran != null) {
try {
tran.rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
/**
* 对象读取测试(Select方法)
*/
public void testSelect(){
String hql = " from TUser where name='byf'";
String hql2 = " from TGroup where name='admin'";
try {
List userList = session.createQuery(hql).list();
TUser user = (TUser)userList.get(0);
System.out.println("user name is " + user.getName());
System.out.println("user group is " + user.getGroup().getName());
Assert.assertEquals(user.getName(), "byf");
List groupList = session.createQuery(hql2).list();
TGroup group = (TGroup)groupList.get(0);
System.out.println("group name is " + group.getName());
System.out.println("group user is " + group.getUser().getName());
Assert.assertEquals(group.getUser().getName(), "byf");
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
}