Hibernate组件属性

组件属性,即在实体类中的某个属性属于用户自定义类的对象,具体实现如下

首先创建两个实体类,Address和Students,其中Students类中包含Address类的引用

package com.entity;

import java.util.Date;

/**
 * 学生类
 * @author user
 *
 */
public class Students {

	private int sid;//学号
	private String sname;//姓名
	private String gender;//性别
	private Date birthday;//出生日期
	private Address address;//组件属性(Address为自定义类)
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	
	
}


package com.entity;

/**
 * 地址类
 * @author user
 *
 */
public class Address {

	private String postcode;//邮编
	private String phone;//电话
	private String address;//地址
	
	public Address()
	{

	}

	public String getPostcode() {
		return postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

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

	public Address(String postcode, String phone, String address) {
		super();
		this.postcode = postcode;
		this.phone = phone;
		this.address = address;
	}
	
	
}

接着配置Students的对象关系映射文件
<?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, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<hibernate-mapping >
    <class name="com.entity.Students" table="students">
        <id name="sid"  type="int">
            <generator class="native"/>
        </id>
        <property name="sname" type="string"/>
        <property name="gender" type="string"/>
        <property name="birthday" type="date"/>
        
        <!--配置组件属性,name值为引用的属性名,class为组件所在类  -->
        <component name="address" class="com.entity.Address">
        <!--property的name值为Address类的属性  -->
        	<property name="postcode" type="string"/>
        	<property name="phone" type="string"/>
        	<property name="address" type="string"></property>
        </component>
    </class>
</hibernate-mapping>

然后配置Hibernate配置文件
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory >
		
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///test</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		
		<mapping resource="com/entity/Students.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

测试往数据库添加数据
package com.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.entity.Address;
import com.entity.Students;

public class StudentsTest {

	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	//初始化方法
	@Before
	public void init()
	{
		//创建配置对象
		Configuration configuration=new Configuration().configure();
		//创建服务注册对象
		ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		//创建会话工厂对象
		sessionFactory=configuration.buildSessionFactory(serviceRegistry);
		//会话对象
		session=sessionFactory.openSession();
		//开启事务
		transaction=session.beginTransaction();
		
		
	}
	
	@Test
	public void testSaveStudents()
	{
		Students student=new Students();
		student.setSname("张三");
		student.setGender("男");
		student.setBirthday(new Date());
		
		//创建Address类实例
		Address address=new Address("710068", "13088888888", "北京");
		student.setAddress(address);
		
		session.save(student);
	}
	
	@After
	public void destroy()
	{
		transaction.commit();//提交事务
		session.close();//关闭会话
		sessionFactory.close();//关闭会话工厂
	}
}

运行testSaveStudents方法,即可往数据库添加记录


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值