您的位置:首页 > 其它

Hibernate Component映射的一个简单例子

2013-01-15 15:06 417 查看


建立一个相关的数据库表:

create table c_user(
id int(11) not null auto_increment,
age int(11) default null,
firstname varchar(50) default null,
lastname varchar(50) default null,
address varchar(200) default null,
zipcode varchar(10) default null,
tel varchar(20) default null,
primary key(id)
)engine=innodb default charset=gbk;

注解:该表是一个用户信息表,可以将用户信息归纳为两个部分:一个部分是name(姓名),包含firstname(姓)和lastname(名);另一部分是contact(联系方式),包括了address(地址)、zipcode(邮编)和tel(电话)等信息。在创建实体类时,可以将name和contact分别封装到2个独立的类中,这样就提高了系统的复用性和灵活性。也就是说,需要使用Component映射,将其他的实体类映射在一起。

建立一个Name.java类:

package collect.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;
    }
    
}

建立联系方式相关的类Contact.java:

package collect.component;

import java.io.Serializable;

public class Contact implements Serializable{

	private String address;
	private String zipcodes;
	private String tel;

	public Contact() {}
	
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getZipcodes() {
		return zipcodes;
	}

	public void setZipcodes(String zipcodes) {
		this.zipcodes = zipcodes;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

}

最后建立数据库表对应的实体类Cuser.java:

package collect.component;

public class Cuser {
	private Integer id;
	private Integer age;
	private Name name;
	private Contact contact;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Name getName() {
		return name;
	}

	public void setName(Name name) {
		this.name = name;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

}

建立映射文件Cuser.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="collect.component.Cuser" table="c_user" catalog="ssh">
        <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="collect.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="collect.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>

注解:映射文件使用component元素将Name类、Contact类同数据库表c_user联系起来。

将该映射文件加入到Hibernate的配置文件中,建立一个测试类Test.java:

package collect.component;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {

	public static void main(String[] args) {
		// Configuration管理Hibernate配置
		Configuration config = new Configuration().configure();
		// 根据Configuration建立 SessionFactory
		// SessionFactory用来建立Session
		SessionFactory sessionFactory = config.buildSessionFactory();

		// 创建实例
		Name name=new Name();
		name.setFirstname("闫");
		name.setLastname("术卓");
		
		Contact contact = new Contact();
        contact.setAddress("北京");
        contact.setTel("42689334");
        contact.setZipcodes("100085");
        
		Cuser user= new Cuser();
		user.setAge(33);
		user.setName(name);
		user.setContact(contact);
		
		// 定义主键变量
		Integer pid;

		// 添加数据
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			// 创建主键变量
			pid = (Integer) session.save(user);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

		// 关闭sessionFactory
		sessionFactory.close();

	}

}



运行结果:

控制台:

20:47:57,366 DEBUG SQL:346 - insert intossh.c_user (age, firstname, lastname, address, zipcode, tel) values (?, ?, ?,?, ?, ?)

数据库:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: