码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate笔记②--hibernate类生成表、id生成策略、级联设置、继承映射

时间:2015-07-25 22:56:46      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

一、多表的一个关联关系

老师和学生是一对多的关系

student:tid属性 外键约束 对应teacher表中的id属性

teacher:id

在myeclipse的db窗口中选中两个表来生成类。

?

写一个CRUD

//老师和学生实体保存

????public void save(){

????????Teacher t=new Teacher();

????????t.setName("彭老师");

????????

????????Student s1=new Student();

????????s1.setName("郭靖");

????????

????????Student s2=new Student();

????????s2.setName("杨康");

????????

????????s1.setTeacher(t);

????????s2.setTeacher(t);

????????

????????Set<Student> ss=new HashSet<Student>();

????????ss.add(s1);

????????ss.add(s2);

????????t.setStudents(ss);

????????

????????Session session=HibernateSessionFactory.getSession();

????????Transaction tx=session.beginTransaction();

????????session.save(s1);

????????session.save(s2);

????????tx.commit();

????????HibernateSessionFactory.closeSession();

????}

?

此时实体是保存不进去的,需要在学生实体的hbm.xml配置文件中的many-to-one 标签中添加cascade="all" 设置级联级别为all

?

<many-to-one name="teacher" class="com.hibernate.entity.Teacher" fetch="select" cascade="all">

<column name="tid" />

</many-to-one>

此时数据才可插入成功。老师和学生都可以保存。

?

①对象的三种状态

1临时状态:

使用new命令开辟内存空间的Java对象,在内存中孤立存在

2持久状态:

数据库中存在。

3游离状态

与Session关联的对象

?

由类和配置文件生成表的类

????Configuration cfg = new Configuration().configure("/hibernate.cfg.xml");

????????SchemaExport export = new SchemaExport(cfg);

????????export.create(true, true);

?

?

手写一个配置文件

Stu.hbm.xml

????<class name="com.hibernate.model.Stu" table="t_stu">

????????<id name="id">

????????????<column name="id"></column>

????????????<generator class="uuid"></generator>

????????</id>

????????

????????<property name="name"></property>

????????<property name="age"></property>

????????<many-to-one name="teacher" class="com.hibernate.model.Teacher" cascade="save-update">

????????????<column name="tid"></column>

????????</many-to-one>

????</class>

?

Theacher.hbm.xml

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

?

<hibernate-mapping>

????<class name="com.hibernate.model.Teacher" table="t_table">

????????<id name="id">

????????????<column name="id"/>

????????????<generator class="uuid"></generator>

????????</id>

????????<property name="name"></property>

????????<set name="stus" cascade="delete">

????????????<key>

????????????????<column name="tid"></column>

????????????</key>

????????????<one-to-many class="com.hibernate.model.Stu"/>

????????</set>

????</class>

</hibernate-mapping>

?

还要在hibernate.cfg.xml

添加:

????<mapping resource="com/hibernate/model/Teacher.hbm.xml" />

????<mapping resource="com/hibernate/model/Stu.hbm.xml" />

?

?

三 id的生成策略

<id name="实体类属性名" type="java.lang.Integer">

<column name="对应表中主键字段名" />

<generator class="assiged|increment|identity|native|........" />

</id>

generator class的属性如下:

  1. identity 数据库自动增长的 Oracle数据库不支持

②increment 程序调用增长的 select max(id)from table

他们的共性是都是自增长,程序员无序指定属性值。

③uuid 通过Java.util.uuid类生成的唯一的标识符,

④native 将主键的生成交给数据库,hibernate不管。

⑤assigned 在插入主键的时候由程序处理。

⑥sequence 调用底层数据库序列生成主键,适用于Oracle

?

?

四cascade的属性

  1. save-update当保存或修改对象时,级联保存所有与之关联的临时对象,级联更新所有与之关联的托管对象。
  2. delete 如果老师删了的话 学生也就没了。级联删除所有与之关联的对象。
  3. all 包括save-update和delete 的所有属性

    ?

?

inverse 老师放弃维护关系,级联保存的时候

节省update的语句

指定谁来维护关联关系,不是必须的,在关联关系中,通常让多的那一方来维护关联关系。

?

?

?

五继承关系映射

鉴别器

?

product book ps

三种方式 类的结构相同 配置文件不同导致 映射出来的表结构不一样。

?

第一种方式 不同对象生成的一张表,表中的字段会因类型的不同留空

<hibernate-mapping>

????<class name="com.hibernate.entity.Product" table="t_product" discriminator-value="A">

????????<id name="id" >

????????????<column name="id" />

????????????<generator class="uuid"></generator>

????????</id>

????????<discriminator column="type" type="string"></discriminator>

????????<property name="name"></property>

????????<property name="price"></property>

????????<subclass name="com.hibernate.entity.Book" discriminator-value="B">

????????????<property name="author"></property>

????????</subclass>

????????<subclass name="com.hibernate.entity.Ps" discriminator-value="P">

????????????<property name="handler"></property>

????????</subclass>

????????</class>

</hibernate-mapping>

?

第二种方式

每一个类建立自己的一张表 ,表间的一对一关系用外键关联描述

Jioned-subclass 来描述子表特有的属性

<hibernate-mapping>

????<class name="com.hibernate.entity.Product" table="t_product">

????????<id name="id" type="java.lang.String">

????????????<generator class="uuid"/>

????????</id>

????????<property name="name" type="java.lang.String">

????????????<column name="name" length="20"/>

????????</property>

????????<property name="price" type="java.lang.Double"></property>

????????<joined-subclass name="com.hibernate.entity.Book" table="t_book">

????????????<key column="bid"></key>

????????????<property name="author"></property>

????????</joined-subclass>

????????

????????<joined-subclass name="com.hibernate.entity.Ps" table="t_Ps">

????????????<key column="pid"></key>

????????????<property name="handler"></property>

????????</joined-subclass>

????</class>

</hibernate-mapping>

?

第三种方式 将每个类的所有属性都建立一个表,(包含着父类的所有属性)

Unioned-subclass

<hibernate-mapping>

<class name="com.hibernate.entity.Product" table="t_product">

<id name="id" type="java.lang.String">

<generator class="uuid" />

</id>

?

<property name="name" type="java.lang.String">

<column name="NAME" length="20" />

</property>

?

<property name="price" type="java.lang.Double">

</property>

?

<union-subclass name="com.hibernate.entity.Book" table="t_book">

????<property name="author"></property>

</union-subclass>

<union-subclass name="com.hibernate.entity.Ps" table="t_ps">

????<property name="handler"></property>

</union-subclass>

</class>

</hibernate-mapping>

?

使用crud进行测试

package com.hibernate.dao;

?

import java.util.HashSet;

import java.util.Set;

?

import org.hibernate.Hibernate;

import org.hibernate.Session;

import org.hibernate.Transaction;

?

import com.hibernate.entity.Book;

import com.hibernate.entity.Product;

import com.hibernate.entity.Ps;

import com.hibernate.model.Teacher;

import com.hibernate.model.Stu;

import com.hibernate.util.HibernateSessionFactory;

?

public class CRUD {

????//老师和学生实体保存

????public void save(){

????????Teacher t=new Teacher();

????????t.setName("彭老师");

????????

????????Stu s1=new Stu();

????????s1.setName("郭靖");

????????

????????Stu s2=new Stu();

????????s2.setName("杨康");

????????

????????s1.setTeacher(t);

????????s2.setTeacher(t);

????????

????????Set<Stu> ss=new HashSet<Stu>();

????????ss.add(s1);

????????ss.add(s2);

????????t.setStus(ss);

????????

????????Session session=HibernateSessionFactory.getSession();

????????Transaction tx=session.beginTransaction();

????????session.save(s1);

????????session.save(s2);

????????tx.commit();

????????HibernateSessionFactory.closeSession();

????}

????//保存 product对象

????public void save2(){

????????Product p=new Product();

????????p.setName("产品");

????????p.setPrice(100.00);

????????Book b=new Book();

????????b.setName("钢铁是怎样练成的");

????????b.setPrice(99.9);

????????b.setAuthor("奥斯特洛夫斯基");

????????

????????Ps p1=new Ps();

????????p1.setName("play station");

????????p1.setPrice(230);

????????p1.setHandler("尼古拉");

????????

????????Session session=HibernateSessionFactory.getSession();

????????Transaction tx=session.beginTransaction();

????????session.save(p);

????????session.save(b);

????????session.save(p1);

????????tx.commit();

????????HibernateSessionFactory.closeSession();

????}

????

????//将product对象 查找出来

????public void query(){

????????Session session=HibernateSessionFactory.getSession();

????????Product p=(Product)session.get(Product.class, "4028d0814ec3de48014ec3de49950002");

????????System.out.println(p.getPrice());

????????Book b=(Book)p;

????????System.out.println(b.getAuthor());

????????HibernateSessionFactory.closeSession();

????????

????}

????public static void main(String[] args) {

????????CRUD crud=new CRUD();

//????????crud.save2();

????????crud.query();

????}

}

?

?

?

?

Hibernate笔记②--hibernate类生成表、id生成策略、级联设置、继承映射

标签:

原文地址:http://www.cnblogs.com/chengzhipcx/p/4676715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!