Hibernate组件属性
2016-06-19 22:13
253 查看
组件属性,即在实体类中的某个属性属于用户自定义类的对象,具体实现如下
首先创建两个实体类,Address和Students,其中Students类中包含Address类的引用
接着配置Students的对象关系映射文件
然后配置Hibernate配置文件
测试往数据库添加数据
运行testSaveStudents方法,即可往数据库添加记录
首先创建两个实体类,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 Oracle sequence的使用技巧
- jsp Hibernate批量更新和批量删除处理代码
- jsp hibernate的分页代码第1/3页
- Struts2+Hibernate实现数据分页的方法
- Hibernate环境搭建与配置方法(Hello world配置文件版)
- JAVA+Hibernate 无限级分类
- SSH整合中 hibernate托管给Spring得到SessionFactory
- jsp hibernate 数据保存操作的原理
- hibernate中的增删改查实现代码
- 解决hibernate+mysql写入数据库乱码
- java优化hibernate性能的几点建议
- java Hibernate延迟加载
- hibernate 常用方法介绍
- 详解Java的Hibernate框架中的注解与缓存
- 浅析Java的Hibernate框架中的继承关系设计
- Hibernate实现批量添加数据的方法
- Hibernate4在MySQL5.1以上版本创建表出错 type=InnDB
- JQuery+Ajax+Struts2+Hibernate框架整合实现完整的登录注册
- SSH框架网上商城项目第16战之Hibernate二级缓存处理首页热门显示
- 深入理解Hibernate中的flush机制