将实体对象中的逻辑组成称为component。在配置文件中,使用component节点对组件进行声明。
例如,一个用户有两个部分组成,姓名和联系方式。姓名和联系方式都可以作为用户的组成部分。
1.表结构如下
use sample;
DROP TABLE t_user;
CREATE TABLE t_user (
id INT NOT NULL AUTO_INCREMENT
, age INT
, firstname VARCHAR(50)
, lastname VARCHAR(50)
, address VARCHAR(200)
, zipcode VARCHAR(10)
, tel VARCHAR(20)
, PRIMARY KEY (id)
);
2.配置文件
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>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="cn.blogjava.component.TUser" table="t_user" catalog="sample">
<id name="id" type="integer">
<column name="id" />
<generator class="native" />
</id>
<property name="age" type="integer">
<column name="age" />
</property>
<component name="name" class="cn.blogjava.component.Name">
<property name="firstname" type="string">
<column name="firstname" length="50" />
</property>
<property name="lastname" type="string">
<column name="lastname" length="50" />
</property>
</component>
<component name="contact" class="cn.blogjava.component.Contact">
<property name="address" type="string">
<column name="address" length="200" />
</property>
<property name="zipcodes" type="string">
<column name="zipcode" length="10" />
</property>
<property name="tel" type="string">
<column name="tel" length="20" />
</property>
</component>
</class>
</hibernate-mapping>
将TUser.hbm.xml配置文件加入到hibernate.cfg.xml中去
3.POJO类
Contact.java
package cn.blogjava.component;
import java.io.Serializable;
public class Contact implements Serializable {
private String address;
private String zipcodes;
private String tel;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getZipcodes() {
return zipcodes;
}
public void setZipcodes(String zipcodes) {
this.zipcodes = zipcodes;
}
}
Name.java
package cn.blogjava.component;
import java.io.Serializable;
public class Name implements Serializable {
private String firstname;
private String lastname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
TUser.java
package cn.blogjava.component;
public class TUser implements java.io.Serializable {
private Integer id;
private Integer age;
private Name name;
private Contact contact;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
}
4.测试类
package cn.blogjava.component;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import junit.framework.TestCase;
import junit.framework.Assert;
public class HibernateTest extends TestCase {
Session session = null;
protected void setUp(){
try {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
// delete();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
protected void tearDown() {
try {
session.close();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void delete(){
Transaction tran = null;
String hql = " delete TUser";
try {
tran = session.beginTransaction();
Query query = session.createQuery(hql);
int ret = query.executeUpdate();
System.out.println("Delete records => " + ret);
tran.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
if(tran != null) {
try {
tran.rollback();
} catch(HibernateException e2) {
e2.printStackTrace();
}
}
}
}
public void testInsert(){
Transaction tran = null;
try {
System.out.println("session is " + session);
tran = session.beginTransaction();
//user1
TUser user = new TUser();
user.setAge(25);
Name name = new Name();
name.setFirstname("yu");
name.setLastname("yy");
user.setName(name);
Contact contact = new Contact();
contact.setAddress("dalian");
contact.setTel("42689334");
contact.setZipcodes("116023");
user.setContact(contact);
session.save(user);
//user2
TUser user2 = new TUser();
user2.setAge(26);
Name name2 = new Name();
name2.setFirstname("bai");
name2.setLastname("yf");
user2.setName(name2);
Contact contact2 = new Contact();
contact2.setAddress("beijing");
contact2.setTel("12345678");
contact2.setZipcodes("100010");
user2.setContact(contact2);
session.save(user2);
session.flush();
tran.commit();
Assert.assertEquals(user.getId().intValue() > 0, true);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
if(tran != null) {
try {
tran.rollback();
} catch(HibernateException e2) {
e2.printStackTrace();
}
}
}
}
public void testSelect(){
String hql = " from TUser where age=25 ";
try {
List userList = session.createQuery(hql).list();
TUser user = (TUser)userList.get(0);
Contact contact = user.getContact();
Assert.assertEquals(contact.getAddress(), "dalian");
Assert.assertEquals(user.getName().getFirstname(), "yu");
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
}