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. python学习之 字符串前'r'的用法

    在打开文件的时候open(r'c:\....') 加r和不加''r是有区别的 'r'是防止字符转义的 如果路径中出现'\t'的话 不加r的话\t就会被转义 而加了'r'之后'\t'就能保留原有的样子 ...

  2. Oracle序列和索引

    序列和索引 一.序列 1.序列的概念: 序列(Sequence)是用来生成连续的整数数据的对象.它常常用来作为主键的增长列,可以升序,也可以降序. 2.创建序列: 语法:创建序列           ...

  3. LeetCode Kth Largest Element in an Array

    原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...

  4. 如何把自己打造成技术圈的papi酱

    最近半年,一个叫papi酱的平胸女子连续在微博.朋友圈.创业圈刷屏,当之无愧成了中文互联网的第一大网红.呃,你以为我会巴拉巴拉说一堆网工创业的事?NO,今天想借papi酱的话题跟大家一起聊聊程序员如何 ...

  5. 事务复制-大批量DEL操作

    同事讨论删除发布表历史记录,导致订阅端数据滞后N小时.后来询问得知,发布表T只保留最近31天的数据,每天由Job删除31天前的数据,每天的删除量约400-500万条.默认情况下,在发布端删除400万条 ...

  6. 20151208_使用windows2012配置weblogic节点管理器

    经过实践,weblogic节点管理器的作用主要有两点: 1.可通过weblogic控制台远程控制被管server启停. 2.可以自动重启被管server的进程,并且对spring框架提供比直接启动更快 ...

  7. Android项目:proguard混淆第三方jar.

    1.Gson混淆 ## ---------------------------------- ##   ########## Gson混淆    ########## ## ------------- ...

  8. Java Spring DI之旅

    做过.NET的人很多都用过Microsoft Enterprise Library,里面有一个Dependency injection工具Unity,我们可以使用它来实现依赖注入:什么是依赖注入呢?我 ...

  9. viewpager实现酷炫侧滑demo

    晚上叫外卖,打开饿了么,发现推了一个版本,更新以后,点开了个鸡腿,哇,交互炫炸了. 不过还是有槽点.我是无意中才发现可以左右滑动的.这...你不告诉我,我怎么知道左右可以滑. https://gith ...

  10. JAVA枚举

    枚举类型的使用: 定义一个枚举变量: enum Day{ MOD,TUE,WED,THR,FRI; }; 可以直接输出枚举变量的字符串形式和利用switch输出,在JDK1.6及之前switch只识别 ...