本节内容:

1    简介组件属性

2    demo

1    简介组件属性:

<component  name = "address" class = "Address">

<property name = "postcard" column  = "POSTCARD"></property>

<property name = "address" column = "ADDRESS"></property>

</component>

hibernate 组件component标签 其实就是Student实体类中有Address对象。

2    demo:

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
     <property name="connection.url">jdbc:mysql://localhost:3306/bendi</property>
     <property name="connection.username">root</property>
     <property name="connection.password"></property>
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
     
     <property name="show_sql">true</property>
     <property name="format_sql">true</property>
     <property name="hbm2ddl.auto">create</property>
  
  <mapping resource = "Student.hbm.xml"/>     
    </session-factory>
</hibernate-configuration>

Student.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-12-20 0:42:12 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.ddwei.student.Student" table="STUDENT">
        <id name="pid" type="int">
            <column name="PID" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="SEX" />
        </property>
        <property name="birthday" type="date">
<!--         <property name="birthday" type="time"> -->
<!--         <property name="birthday" type="timestamp"> -->
            <column name="BIRTHDAY" />
        </property>
<!--         <property name="address" type="java.lang.String"> -->
<!--             <column name="ADDRESS" /> -->
<!--         </property> -->
        <property name="picture" type="java.sql.Blob">
            <column name="Picture" />
        </property>
        <component name="address" class="com.ddwei.student.Address">
         <property name="postcode" column="POSTCODE"/>
         <property name="phone" column="PHONE"/>
         <property name="address" column="ADDRESS"/>
        </component>
       
    </class>
</hibernate-mapping>

StudentTest.java

package hibernate_001;

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Blob; import java.sql.SQLException; import java.util.Date;

import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test;

import com.ddwei.student.Address; import com.ddwei.student.Student;

public class StudentTest {    private SessionFactory sessionFactory;  private Session session;  private Transaction trasaction;    @Test  public void testSaveStudent(){ //  Student student =new Student(1,"周恩来","男",new Date(),"绍兴");//创建学生对象   Student student = new Student();   student.setName("秦始皇");   student.setSex("男");   student.setBirthday(new Date()); //  student.setAddress("阿房宫");   Address address = new Address("105789","18912345678","河北敦煌教区");   student.setAddress(address);   session.save(student);//会话保存学生对象进入数据库  }                                              @Before  public void init(){   //1  创建配置对象   Configuration config = new Configuration().configure();   //2  创建服务对象   ServiceRegistry serviceRe = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();      //3  创建会话工厂   sessionFactory = config.buildSessionFactory(serviceRe);      //4  打开会话   session = sessionFactory.openSession();   //5  创建事务   trasaction = session.beginTransaction();  }      @After  public void destroy(){   trasaction.commit();   session.close();   sessionFactory.close();  }

}

Student.java

package com.ddwei.student;

import java.sql.Blob; import java.util.Date;

public class Student {

// java beans 的设计原则  /**   * 1 公有的类 2 共有不带参数构造方法 3 私有属性 4 属性setter/getter方法   */

private int pid;// 学号  private String name;// 姓名  private String sex;// 性别  private Date birthday;// 出生日期  private Address address;// 家庭地址  private Blob picture;//相片

public Student() {

}

public Student(int pid, String name, String sex, Date birthday) {   // super();   this.pid = pid;   this.name = name;   this.sex = sex;   this.birthday = birthday; //  this.address = address;  }

@Override  public String toString() {   return "Student [pid=" + pid + ", name=" + name + ", sex=" + sex     + ", birthday=" + birthday + ", address=" + address + "]";  }

public int getPid() {   return pid;  }

public void setPid(int pid) {   this.pid = pid;  }

public Date getBirthday() {   return birthday;  }

public void setBirthday(Date birthday) {   this.birthday = birthday;  }

public Address getAddress() {   return address;  }

public void setAddress(Address address) {   this.address = address;  }

public String getName() {   return name;  }

public void setName(String name) {   this.name = name;  }

public String getSex() {   return sex;  }

public void setSex(String sex) {   this.sex = sex;  }

public Blob getPicture() {   return picture;  }

public void setPicture(Blob picture) {   this.picture = picture;  }

}

Address.java

package com.ddwei.student;

//地址类 public class Address {    private String postcode;//邮编  private String phone;//电话  private String address;//地址    public String getPostcode() {   return postcode;  }  public void setPostcode(String postcode) {   this.postcode = postcode;  }  public String getPhone() {   return phone;  }  public void setPhone(String phone) {   this.phone = phone;  }  public String getAddress() {   return address;  }  public void setAddress(String address) {   this.address = address;  }    //无参构造方法  public Address(){     }        //有参构造方法  public Address(String postcode, String phone, String address) {   super();   this.postcode = postcode;   this.phone = phone;   this.address = address;  } }

hibernate课程 初探单表映射3-4 组件属性的更多相关文章

  1. hibernate课程 初探单表映射4-1 课程总结

    ORM是一种面向对象编程的方法,用这种方法来避免写数据库底层语言sql语句,这样有利于java的跨平台,扩展.维护.而hirenate是ORM的一种框架 hirbernate开发基本步骤编写配置文档h ...

  2. hibernate课程 初探单表映射3-1 hibernate单表操作简介

    本章简介: 1 单一主键 2 基本类型 3 对象类型 4 组件属性 5 单表操作CRUD实例

  3. hibernate课程 初探单表映射1-9 创建关系映射文件

    创建关系映射文件:(把实体类映射成一个表) 1 右键src==>new==>other==>hibernate==>hbm.xml==>Student==>Fini ...

  4. hibernate课程 初探单表映射3-3 对象类型

    本节简介: 1 简介对象类型(重点是音视频blob类型) 2 demo(对图片的写入数据库与读取) 1 简介对象类型 映射类型 java类型 标准sql类型 mysql类型 oracle类型 bina ...

  5. hibernate课程 初探单表映射2-7 hbm配置文件常用设置

    本节主要简介hbm配置文件以下内容: 1 mapping标签 2 class标签 3 id标签 1 hibbernate-mapping标签 schema 模式名称 catalog 目录名称 defa ...

  6. hibernate课程 初探单表映射2-4 transaction简介

    1 hibernate是非自动提交.如果transaction不写的话,会只创建表结构而不插入语句.   如果不写transaction而想实现插入的功能的话,需要重写session的dowork方法 ...

  7. hibernate课程 初探单表映射2-2 hibernate常用配置

    1 hibernate.cfg.xml常用配置: show_sql 控制台打印sql format_sql 控制台将sql排版 hbm2ddl.auto: create 删除表结构,重新建表并插值 u ...

  8. hibernate课程 初探单表映射2-1 hibernate进阶 本章简介

    本章简介,主要讲5大块的内容 1 hibernate.cfg.xml的配置 2 session 的简介 3 transaction的简介 4 session的详解 5 对象关系映射常用配置

  9. hibernate课程 初探单表映射1-11 通过hibernate API访问编写第一个小例子

    hibernate 业务流程 1 创建配置对象 Configuration config  = new  Configuration().configure(); 2 创建服务注册对象 Service ...

随机推荐

  1. eos各表的关系

    在使用EOS WorkFlow的过程中,无论是开发者在“开发环境”中定义业务流程,还是“工作流引擎”控制流程流转,或是工作流参与者使用的“客户端”,再或者管理员使用的“管理与监控工具”,在这期间都会贯 ...

  2. 【Qt文档阅读】事件系统

    在Qt中,事件对象都继承于QEvent类,它表示应用程序内部或由于应用程序需要了解的外部活动而发生的事情.事件可以由QObject子类的任何实例接收和处理,尤其是widget.本文档描述如何在典型应用 ...

  3. [CentOS7] firewalld重启失败 Failed to start firewalld - dynamic firewall daemon.

    错误信息: Failed to start firewalld - dynamic firewall daemon. 如图: 解决方法: 也就是kernel不支持现在的firewall防火墙的某些模块 ...

  4. 基于php双引号中访问数组元素

    关于 php访问数组 {} []

  5. [Django笔记] models 深入学习

    对着官方文档撸一遍,顺便做点笔记 models 定义了本应用的数据库表结构.底层可以由不同的数据库封装实现,因为不同的数据库字段类型不一样,因此,跟以往直接用单一数据库(如mysql)建立的应用有很大 ...

  6. 洛谷P3649 [APIO2014]回文串(回文自动机)

    传送门 话说回文自动机我自己都还没搞懂呢…… 等到时候会了再来填坑 //minamoto #include<cstdio> #include<cstring> #define ...

  7. 解读人:闫克强,Metabolic and gut microbial characterization of obesity-prone mice under high-fat diet(高脂饮食下易胖倾向小鼠的代谢和肠道微生物菌群特征分析)

    单位: 上海中医药大学 蚌埠医学院 上海交通大学附属第六人民医院 夏威夷大学癌症中心 第二军医大学 技术:非靶向代谢组学,16S rRNA测序技术 一. 概述: 本研究对小鼠进行高脂饮食,根据体重增长 ...

  8. vs.net远程调试

    有些时候,不能在本机器启动程序进行调试,例如调试全屏模式,或者调试那些需要在特定运行环境的程序,这时候就只能进行远程调试了. 一般的调试器都支持远程调试,vs也不例外.只需要在远程机器上启动一个应用程 ...

  9. 通过jQuery实现AJAX

    通过jQuery实现AJAX > 使用get和getJSON都会有缓存问题,并且使用get方法不能传送较多的数据. 问题: 在IE浏览器中,get请求使用ajax存在缓存问题,会使用上一次请求的 ...

  10. C#正则表达式快速入门

    作者将自己在学习正则表达式中的心得和笔记作了个总结性文章,希望对初学C#正则表达式的读者有帮助. [内容] 什么是正则表达式 涉及的基本的类 正则表达式基础知识 构建表达式基本方法 编写一个检验程序 ...