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. Windows下mock环境搭建-加速项目Api开发

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 公司进行技术部拆分,以项目制作为新的开发模式,前端+移动端+后端,于是加速Api开发变得很有必要,准 ...

  2. ListView简单使用

    先上效果: 主要实现了Listview的绑定和点击事件.项目资源结构如下: 先创建一个动物类,用来装载数据: Animal类如下: package com.example.simplelistview ...

  3. VS2015——命令行下编译、静态库动态库制作以及断点调试

    c程序编译流程 程序的基本流程如图: 1. 预处理 预处理相当于根据预处理指令组装新的C/C++程序.经过预处理,会产生一个没有宏定义,没有条件编译指令,没有特殊符号的输出文件,这个文件的含义同原本的 ...

  4. ionic下拉加载自动触发

    ionic提供的下拉加载,是要滑动去下拉加载,没有提供api自动触发下拉加载,比如刚进页面,或者切换tab时想触发一次下拉加载. 添加如下service: angular.module('YourAp ...

  5. 创建一个程序,从应用程序中随机添加N名参加歌唱比赛的同学,并随机对这N名同学的比赛按姓名的拼音先后顺序进行排序

    public class Pint { /** * 姓名 */ public String name; /** * 年龄 */ public String age; public Pint(){ } ...

  6. NET基础(1):类型基础

    所有类型都从System.Object 派生,‘运行时’要求每个类型都从System.Object类派生,也就是说,以下两个类型定义完全一致: //隐式派生字Object class Employee ...

  7. ios -- 教你如何轻松学习Swift语法(一)

    目前随着公司开发模式的变更,swift也显得越发重要,相对来说,swift语言更加简洁,严谨.但对于我来说,感觉swift细节的处理很繁琐,可能是还没适应的缘故吧.基本每写一句代码,都要对变量的数据类 ...

  8. php操作Mysql 以及封装常用的函数 用外连接连接3个表的案例

    <?php header("content-type;text/html;charset=utf-8"); //数据库连接define('DB_HOST','localhos ...

  9. BAT脚本打印空行的使用方法

    @echo off echo= echo, echo; echo+ echo/ echo[ echo] echo: echo. echo\ pause 这十种方法可以分为三组,每组的效率依次递减. 至 ...

  10. opencv的学习笔记2

    继续昨晚的学习总结,昨晚看到轨迹条的创建就没有看下去了,今天继续: 1.轨迹条的创建: 轨迹条往往会和一个回调函数配合使用,当轨迹条发生改变,就调用这个轨迹条的回调函数 int createTrack ...