对于ORM而言,一个重要的特性就是对实体之间关联关系的管理。数据关联是ORM一个重要的特征,但往往也是导致性能低下的原因,在开发中要特别注意这一点。
一对一关联有两种类型:主键关联和唯一外键关联
一.主键关联:
一对一主键关联形式,即两张关联表通过主键形成一对一映射关系。
例如,一个中国公民只能有一份护照。
1.数据模型如下
2.表定义sql
DROP
TABLE
T_User;
DROP
TABLE
T_Passport;
CREATE
TABLE
T_Passport (
id
INT
NOT
NULL
, serial
VARCHAR
(
30
)
, expiry
INT
,
PRIMARY
KEY
(id)
);
CREATE
TABLE
T_User (
id
INT
NOT
NULL
AUTO_INCREMENT
, name
VARCHAR
(
50
)
, age
INT
,
PRIMARY
KEY
(id)
,
INDEX
(id)
,
CONSTRAINT
FK_T_User_1
FOREIGN
KEY
(id)
REFERENCES
T_Passport (id)
);
3.POJO类
TUser.java
package cn.blogjava.start;
public class TUser implements java.io.Serializable {
// Fields
private Integer id;
private Integer age;
private String name;
private TPassport passport;
// Constructors
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public TPassport getPassport() {
return passport;
}
public void setPassport(TPassport passport) {
this.passport = passport;
}
/** default constructor */
public TUser() {
}
/** constructor with id */
public TUser(Integer id) {
this.id = id;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
TPassport.java
package cn.blogjava.start;
import java.io.Serializable;
public class TPassport implements Serializable {
private Integer id;
private String serial;
private Integer expiry;
private TUser user;
public Integer getExpiry() {
return expiry;
}
public void setExpiry(Integer expiry) {
this.expiry = expiry;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSerial() {
return serial;
}
public void setSerial(String serial) {
this.serial = serial;
}
public TUser getUser() {
return user;
}
public void setUser(TUser user) {
this.user = user;
}
}
3.配置文件
TUser.hbm.xml
<?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.TUser" table="T_User" catalog="sample">
<id name="id" type="integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="string">
<column name="name" length="100" not-null="true" />
</property>
<property name="age" type="java.lang.Integer" column="age" />
<one-to-one name="passport"
class="cn.blogjava.start.TPassport"
cascade="all"
outer-join="true"
/>
</class>
</hibernate-mapping>
TPassport.hbm.xml
<?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.TPassport" table="T_Passport" catalog="sample">
<id name="id" type="integer" column="id">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="serial" type="string" column="serial" />
<property name="expiry" type="java.lang.Integer" column="expiry" />
<one-to-one name="user"
class="cn.blogjava.start.TUser"
constrained="true"
/>
</class>
</hibernate-mapping>
4.测试代码
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();
}
}
public void testInsert() {
Transaction tran = null;
try {
TUser user = new TUser();
user.setName("byf");
user.setAge(new Integer(26));
TPassport passport = new TPassport();
passport.setSerial("PCN123456");
passport.setExpiry(new Integer(20081010));
//设置相互关联
user.setPassport(passport);
passport.setUser(user);
tran = session.beginTransaction();
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'";
try {
List userList = session.createQuery(hql).list();
TUser user = (TUser)userList.get(0);
System.out.println("user name is " + user.getName());
System.out.println("passport serial NO. is " + user.getPassport().getSerial());
Assert.assertEquals(user.getName(), "byf");
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
}
posted on 2006-07-05 15:08
knowhow 阅读(3403)
评论(3) 编辑 收藏 所属分类:
ORM:Hibernate及其他