hibernate hibernate.cfg.xml component 组件
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 组件的更多相关文章
- hibernate配置文件hibernate.cfg.xml和.hbm.xml的详细解释
原文地址:http://blog.csdn.net/qiaqia609/article/details/9456489 hibernate.cfg.xml -标准的XML文件的起始行,version= ...
- hibernate.cfg.xml 配置(摘录)
配置文件中映射元素详解 对象关系的映射是用一个XML文档来说明的.映射文档可以使用工具来生成,如XDoclet,Middlegen和AndroMDA等.下面从一个映射的例子开始讲解映射元素,映射文件的 ...
- 转: hibernate配置文件hibernate.cfg.xml和.hbm.xml的详细解释
http://blog.csdn.net/yuhui123999/article/details/51886531 hibernate.cfg.xml -标准的XML文件的起始行,version='1 ...
- Hibernate入门篇<1>hibernate.cfg.xml学习小结
Hibernate配置文件主要用于配置数据库连接和Hibernate运行时所需的各种属性,这个配置文件应该位于应用程序或Web程序的类文件夹 classes中.Hibernate配置文件支持两种形式, ...
- spring applicationContext.xml和hibernate.cfg.xml设置
applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- hibernate.cfg.xml常见配置
转载自:http://blog.csdn.net/qiaqia609/article/details/9456489 <!--标准的XML文件的起始行,version='1.0'表明XML的版本 ...
- hibernate配置文件hibernate.cfg.xml的详细解释
<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?x ...
- hibernate.cfg.xml配置文件和hbm.xml配置文件
http://blog.sina.com.cn/s/blog_a7b8ab2801014m0e.html hibernate.cfg.xml配置文件格式 <?xml version=" ...
- 问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found解决方法
问题Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not fo ...
随机推荐
- 【Swing】理解Swing中的事件与线程
talk is cheap , show me the code. Swing中的事件 事件驱动 所有的GUI程序都是事件驱动的.Swing当然也是. GUI程序不同于Command Line程序,一 ...
- 控制Wordpress对搜索引擎的可见性
网站通过Robots协议告诉搜索引擎哪些页面可以抓取,哪些页面不能抓取,这些通过robots.txt体现. wordpress本身没有robots.txt,但是用根目录访问/robots.txt,如果 ...
- Robot framework + appium环境搭建
Robot framework+appium环境搭建 首先梳理一下要用到的工具和安装包: 1. Android + JAVA. jdk : http://www.oracle.com/technetw ...
- python实现并行爬虫
问题背景:指定爬虫depth.线程数, python实现并行爬虫 思路: 单线程 实现爬虫类Fetcher 多线程 threading.Thread去调Fet ...
- nmap使用教程
Nmap是一款开源免费的网络发现(Network Discovery)和安全审计(Security Auditing)工具.软件名字Nmap是Network Mapper的简称.Nmap最初是由Fyo ...
- Js动态获取iframe子页面的高度总结
问题的缘由 产品有个评论列表引用的是个iframe,高度不固定于是引发这个总结. 方法1:父级页面获取子级页面的高度 给元素设置高度 这方法是用在父级页面里的,通过获取子级页面的高度给iframe设置 ...
- bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群
网络流,对于每一个行星,将行星所在行到行星连一条流量为1的边,将行星到其所在列连一条流量为1的边,从源点到所有行连一条流量为1的边,将所有列到汇点都连一条流量为1的边,最大流即为答案. 代码 #inc ...
- HttpClient_002_中文乱码、HttpClient中文乱码透析、总结
中文乱码原理代码: String s = "中文"; byte[] bs2 = s.getBytes("utf-8");//将s拆成:utf-8个体,注:utf ...
- paper 101:图像融合算法及视觉艺术应用
1:基于泊松方程的图像融合方法,利用偏微分方程实现了不同图像上区域的无缝融合.比较经典的文章: P. Pérez, M. Gangnet, A. Blake. Poisson image editin ...
- java 除法向上,向下取整
向上取整用Math.ceil(double a) 向下取整用Math.floor(double a) // 举例: double a=35; double b=20; double c = a/b; ...