1.为什么使用component组件?

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

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

2.实例

2.1 Java 对象:

  1. public class Person {
  2.  
  3. private int id;
  4. private Name name;
  5. private Parents parents;
  6.  
  7. public Parents getParents() {
  8. return parents;
  9. }
  10.  
  11. public void setParents(Parents parents) {
  12. this.parents = parents;
  13. }
  14.  
  15. public int getId() {
  16. return id;
  17. }
  18.  
  19. public void setId(int id) {
  20. this.id = id;
  21. }
  22.  
  23. public Name getName() {
  24. return name;
  25. }
  26.  
  27. public void setName(Name name) {
  28. this.name = name;
  29. }
  30. }
  1. public class Parents {
  2. private Name father;
  3. private Name mother;
  4.  
  5. public Name getFather() {
  6. return father;
  7. }
  8.  
  9. public void setFather(Name father) {
  10. this.father = father;
  11. }
  12.  
  13. public Name getMother() {
  14. return mother;
  15. }
  16.  
  17. public void setMother(Name mother) {
  18. this.mother = mother;
  19. }
  20. }
  1. public class Name {
  2. private String bigName;
  3. private String nickName;
  4. private int nameVersion;
  5.  
  6. public String getBigName() {
  7. return bigName;
  8. }
  9.  
  10. public void setBigName(String bigName) {
  11. this.bigName = bigName;
  12. }
  13.  
  14. public int getNameVersion() {
  15. return nameVersion;
  16. }
  17.  
  18. public void setNameVersion(int nameVersion) {
  19. this.nameVersion = nameVersion;
  20. }
  21.  
  22. public String getNickName() {
  23. return nickName;
  24. }
  25.  
  26. public void setNickName(String nickName) {
  27. this.nickName = nickName;
  28. }
  29. }

2.2 Hibernate 配置文件 hibernate.cfg.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"
  5. >
  6. <hibernate-configuration>
  7. <session-factory>
  8. <!-- Platform settings -->
  9. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  10. <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
  11. <property name="connection.username">root</property>
  12. <property name="connection.password">123456</property>
  13. <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
  14. <!-- Miscellaneous Settings -->
  15. <property name="show_sql">true</property>
  16. <!--mapping files-->
  17. <mapping resource="test/com/hibernate/config/hbm/person.hbm.xml"/>
  18. </session-factory>
  19.  
  20. </hibernate-configuration>

映射文件person.hbm.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="test.com.hibernate.component.Person" table="person" lazy="false">
  7. <id name="id" column="id" type="java.lang.Integer">
  8. <generator class="native"/>
  9. </id>
  10. <component name="name" class="test.com.hibernate.component.Name" >
  11. <property name="bigName" column="bigName" type="string"/>
  12. <property name="nickName" column="nickName" type="string"/>
  13. <property name="nameVersion" column="nameVersion" type="integer"/>
  14. </component>
  15. <component name="parents" class="test.com.hibernate.component.Parents">
  16. <component name="father" class="test.com.hibernate.component.Name">
  17. <property name="bigName" column="f_bigName" type="string"/>
  18. <property name="nickName" column="f_nickName" type="string"/>
  19. <property name="nameVersion" column="f_nameVersion" type="integer"/>
  20. </component>
  21. <component name="mother" class="test.com.hibernate.component.Name">
  22. <property name="bigName" column="m_bigName" type="string"/>
  23. <property name="nickName" column="m_nickName" type="string"/>
  24. <property name="nameVersion" column="m_nameVersion" type="integer"/>
  25. </component>
  26. </component>
  27. </class>
  28. </hibernate-mapping>

2.3 Hibernate 操作代码:

HibernateUtil.java

  1. public class HibernateUtil {
  2. static SessionFactory sessionFactory = null;
  3. static{
  4. StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
  5. .configure("test/com/hibernate/config/hibernate.cfg.xml").build();
  6. MetadataSources sources = new MetadataSources();
  7. Metadata metadata = sources.buildMetadata(registry);
  8. sessionFactory = metadata.buildSessionFactory();
  9. }
  10. public static Session openSession(){
  11. if(sessionFactory==null){
  12. throw new HibernateException("session factory is null!");
  13. }
  14. return sessionFactory.openSession();
  15. }
  16. public static void destroy() {
  17. if(sessionFactory!=null){
  18. sessionFactory.close();
  19. }
  20. }
  21. }

Test.java

  1. public class Test {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. Name name = new Name();
  6. name.setBigName("Daughter");
  7. name.setNickName("girl");
  8. name.setNameVersion(1);
  9. Person person = new Person();
  10. person.setName(name);
  11.  
  12. Name father = new Name();
  13. father.setBigName("Father");
  14. father.setNickName("man");
  15. father.setNameVersion(1);
  16. Name mother = new Name();
  17. mother.setBigName("Mother");
  18. mother.setNickName("woman");
  19. mother.setNameVersion(1);
  20.  
  21. Parents parents = new Parents();
  22. parents.setFather(father);
  23. parents.setMother(mother);
  24.  
  25. person.setParents(parents);
  26.  
  27. PersonDao.save(person);
  28. }
  29. }

上述例子使用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. cocos2d-x-3.11.1 初使用

    1. 引擎子系统包括: 世界编辑器.渲染系统.人机交互系统.动画系统.音频系统.物理引擎.网络接口 等 2. cocos2d-x 特点:开源的.跨平台的. cocos2d-x的发展过程: cocos2 ...

  2. 【C++】int、const char*、char*、char、string之间的转换

    #include "stdafx.h" #include<string> #include<vector> #include<iostream> ...

  3. c#:浅克隆和深克隆,序列化和反序列化

    一.浅克隆和深克隆(浅复制和深复制)浅克隆和深克隆最典型的应用是数据集对象DataSet的Clone和Copy方法.Clone()方法用来复制DataSet的结构,但是不复制DataSet的数据,实现 ...

  4. (地址)eclipse插件开发攻略的访问地址

    园子地址: http://www.cnblogs.com/liuzhuo/category/257208.html 关键字: Eclipse插件开发彻底攻略 eclipse插件开发基础篇之

  5. ios每日一发--Leanclude数据云存储以及登录 注册账户

    利用LeanCloud来实现注册账号,存储账号以及,登录时查询账号是否正确.集成方式很简单可以看这里的官方文档.地址是这里: https://leancloud.cn/docs/ 在这里创建应用,以及 ...

  6. Web前端开发规范文档

    Web前端开发规范文档 规范目的: 使开发流程更加规范化. 通用规范: TAB键用两个空格代替(windos下tab键占四个空格,linux下TAB键占八个空格). CSS样式属性或者JAVASCRI ...

  7. mongodb的一些基本操作

    1.列出所有数据库 >show dbs   2.使用数据库 >use memo   3.列出当前数据库的collections >show collections   4.显示当前正 ...

  8. zjuoj 3601 Unrequited Love

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3601 Unrequited Love Time Limit: 16 Sec ...

  9. SpringMvc自定义拦截器

    SpringMvc也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义拦截器必须实现HandlerInterceptor接口 -preHandle():这个方法在业务处理器 ...

  10. Sie sind das Essen und wir sind die Jaeger!

    WCF  http://www.cnblogs.com/iamlilinfeng/archive/2012/09/25/2700049.html HTTP 数据库分库分表 读写分离 负载均衡 wind ...