1.为什么使用component组件?

当一个表的列数目比较多时,可以根据属性分类,将一个java对象拆分为几个对象。

数据库还是一张表,不过有多个对象与之对应。

2.实例

2.1 Java 对象:

 public class Person {

     private int id;
private Name name;
private Parents parents; public Parents getParents() {
return parents;
} public void setParents(Parents parents) {
this.parents = parents;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public Name getName() {
return name;
} public void setName(Name name) {
this.name = name;
}
}
 public class Parents {
private Name father;
private Name mother; public Name getFather() {
return father;
} public void setFather(Name father) {
this.father = father;
} public Name getMother() {
return mother;
} public void setMother(Name mother) {
this.mother = mother;
}
}
 public class Name {
private String bigName;
private String nickName;
private int nameVersion; public String getBigName() {
return bigName;
} public void setBigName(String bigName) {
this.bigName = bigName;
} public int getNameVersion() {
return nameVersion;
} public void setNameVersion(int nameVersion) {
this.nameVersion = nameVersion;
} public String getNickName() {
return nickName;
} public void setNickName(String nickName) {
this.nickName = nickName;
}
}

2.2 Hibernate 配置文件 hibernate.cfg.xml

 <?xml version="1.0" encoding="utf-8" ?>
<!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>
<!-- Platform settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Miscellaneous Settings -->
<property name="show_sql">true</property>
<!--mapping files-->
<mapping resource="test/com/hibernate/config/hbm/person.hbm.xml"/>
</session-factory> </hibernate-configuration>

映射文件person.hbm.xml

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="test.com.hibernate.component.Person" table="person" lazy="false">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<component name="name" class="test.com.hibernate.component.Name" >
<property name="bigName" column="bigName" type="string"/>
<property name="nickName" column="nickName" type="string"/>
<property name="nameVersion" column="nameVersion" type="integer"/>
</component>
<component name="parents" class="test.com.hibernate.component.Parents">
<component name="father" class="test.com.hibernate.component.Name">
<property name="bigName" column="f_bigName" type="string"/>
<property name="nickName" column="f_nickName" type="string"/>
<property name="nameVersion" column="f_nameVersion" type="integer"/>
</component>
<component name="mother" class="test.com.hibernate.component.Name">
<property name="bigName" column="m_bigName" type="string"/>
<property name="nickName" column="m_nickName" type="string"/>
<property name="nameVersion" column="m_nameVersion" type="integer"/>
</component>
</component>
</class>
</hibernate-mapping>

2.3 Hibernate 操作代码:

HibernateUtil.java

 public class HibernateUtil {
static SessionFactory sessionFactory = null;
static{
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure("test/com/hibernate/config/hibernate.cfg.xml").build();
MetadataSources sources = new MetadataSources();
Metadata metadata = sources.buildMetadata(registry);
sessionFactory = metadata.buildSessionFactory();
}
public static Session openSession(){
if(sessionFactory==null){
throw new HibernateException("session factory is null!");
}
return sessionFactory.openSession();
}
public static void destroy() {
if(sessionFactory!=null){
sessionFactory.close();
}
}
}

Test.java

 public class Test {

     public static void main(String[] args) {

         Name name = new Name();
name.setBigName("Daughter");
name.setNickName("girl");
name.setNameVersion(1);
Person person = new Person();
person.setName(name); Name father = new Name();
father.setBigName("Father");
father.setNickName("man");
father.setNameVersion(1);
Name mother = new Name();
mother.setBigName("Mother");
mother.setNickName("woman");
mother.setNameVersion(1); Parents parents = new Parents();
parents.setFather(father);
parents.setMother(mother); person.setParents(parents); PersonDao.save(person);
}
}

上述例子使用Hibernate版本为5.2Final,配置文件也可以用以前的Configuration配置。

2.4 运行结果

自动创建一个person表,并插入一条数据

hibernate hibernate.cfg.xml component 组件的更多相关文章

  1. hibernate配置文件hibernate.cfg.xml和.hbm.xml的详细解释

    原文地址:http://blog.csdn.net/qiaqia609/article/details/9456489 hibernate.cfg.xml -标准的XML文件的起始行,version= ...

  2. hibernate.cfg.xml 配置(摘录)

    配置文件中映射元素详解 对象关系的映射是用一个XML文档来说明的.映射文档可以使用工具来生成,如XDoclet,Middlegen和AndroMDA等.下面从一个映射的例子开始讲解映射元素,映射文件的 ...

  3. 转: hibernate配置文件hibernate.cfg.xml和.hbm.xml的详细解释

    http://blog.csdn.net/yuhui123999/article/details/51886531 hibernate.cfg.xml -标准的XML文件的起始行,version='1 ...

  4. Hibernate入门篇<1>hibernate.cfg.xml学习小结

    Hibernate配置文件主要用于配置数据库连接和Hibernate运行时所需的各种属性,这个配置文件应该位于应用程序或Web程序的类文件夹 classes中.Hibernate配置文件支持两种形式, ...

  5. spring applicationContext.xml和hibernate.cfg.xml设置

    applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  6. hibernate.cfg.xml常见配置

    转载自:http://blog.csdn.net/qiaqia609/article/details/9456489 <!--标准的XML文件的起始行,version='1.0'表明XML的版本 ...

  7. hibernate配置文件hibernate.cfg.xml的详细解释

    <!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式-->                 <?x ...

  8. hibernate.cfg.xml配置文件和hbm.xml配置文件

    http://blog.sina.com.cn/s/blog_a7b8ab2801014m0e.html hibernate.cfg.xml配置文件格式 <?xml version=" ...

  9. 问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found解决方法

    问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not fo ...

随机推荐

  1. Nginx配置(日志服务器中关于日志的产生)

    一:概括 1.需要配置的概括 定义日志格式 日志的分割字段:^A 日志格式:IP地址^A服务器时间^A请求参数 配置location,记录请求日志到本地磁盘 将数据按照给定的日志格式存储到本地磁盘 二 ...

  2. wordpress搬家到本地URL修改问题

    把原来服务器上面的WordPress的数据库和目录文件全部备份下来,在本地用xampp搭了一个服务器,然后将数据库和目录文件全部导入,更改conf文件中的数据库账号密码.没想到本地网站的所有CSS样式 ...

  3. svg学习(五)ellipse

    <ellipse> 标签 <ellipse> 标签可用来创建椭圆.椭圆与圆很相似.不同之处在于椭圆有不同的 x 和 y 半径,而圆的 x 和 y 半径是相同的. <?xm ...

  4. Groovy学习笔记(一)

    1.1 安装Groovy Groovy主页:http://www.groovy-lang.org 确保本地系统安装了Java 1.1.1 在Windows系统上安装Groovy 1.创建环境变量GRO ...

  5. Hadoop实战5:MapReduce编程-WordCount统计单词个数-eclipse-java-windows环境

    Hadoop研发在java环境的拓展 一 背景 由于一直使用hadoop streaming形式编写mapreduce程序,所以目前的hadoop程序局限于python语言.下面为了拓展java语言研 ...

  6. Why does pthread_cond_signal not work?【转】

    转自:http://stackoverflow.com/questions/16819169/why-does-pthread-cond-signal-not-work# 0 down vote fa ...

  7. Codeforces 749B:Parallelogram is Back(计算几何)

    http://codeforces.com/problemset/problem/749/B 题意:已知平行四边形三个顶点,求另外一个顶点可能的位置. 思路:用向量来做. #include <c ...

  8. UltraISO向U盘写入镜像特别慢

    电脑:Dell INSPIRON 1416 系统:WIN7旗舰版32位 U盘:金士顿8G 镜像:CentOS7 ×86_64 问题: 开始使用"写入"功能,写入速度72k/s 后来 ...

  9. [充电]多线程无锁编程--原子计数操作:__sync_fetch_and_add等12个操作

    转自:http://blog.csdn.net/minCrazy/article/details/40791795 多线程间计数操作.共享状态或者统计相关时间次数,这些都需要在多线程之间共享变量和修改 ...

  10. websphere如何删除应用程序服务器(概要管理工具)

    在IBM WebSphere 的概要管理工具中我们可以新建一个应用程序服务器,但是工具中并未提供删除已经建过的应用程序服务器.下面 交大家一个比较简单的方法来删除应用程序服务器 图片中可以看到,我已经 ...