您的位置:首页 > 其它

Hibernate组件属性

2016-06-19 22:13 253 查看
组件属性,即在实体类中的某个属性属于用户自定义类的对象,具体实现如下

首先创建两个实体类,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方法,即可往数据库添加记录
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate