一、多表的一个关联关系
老师和学生是一对多的关系
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> name="age"></property> <many-to-one name="teacher" class="com.hibernate.model.Teacher" cascade="save-update"> 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> name="com.hibernate.model.Teacher" table="t_table"> name="id"/> <set name="stus" cascade="delete"> <key> <column </key> <one-to-many class="com.hibernate.model.Stu"/> </set> </hibernate-mapping> |
还要在hibernate.cfg.xml
添加:
<mapping resource="com/hibernate/model/Teacher.hbm.xml" 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的属性如下:
- identity 数据库自动增长的 Oracle数据库不支持
②increment 程序调用增长的 select max(id)from table
他们的共性是都是自增长,程序员无序指定属性值。
③uuid 通过Java.util.uuid类生成的唯一的标识符,
④native 将主键的生成交给数据库,hibernate不管。
⑤assigned 在插入主键的时候由程序处理。
⑥sequence 调用底层数据库序列生成主键,适用于Oracle
四cascade的属性
- save-update当保存或修改对象时,级联保存所有与之关联的临时对象,级联更新所有与之关联的托管对象。
- delete 如果老师删了的话 学生也就没了。级联删除所有与之关联的对象。
- all 包括save-update和delete 的所有属性
inverse 老师放弃维护关系,级联保存的时候
节省update的语句
指定谁来维护关联关系,不是必须的,在关联关系中,通常让多的那一方来维护关联关系。
五继承关系映射
鉴别器
product book ps
三种方式 类的结构相同 配置文件不同导致 映射出来的表结构不一样。
第一种方式 不同对象生成的一张表,表中的字段会因类型的不同留空
name="com.hibernate.entity.Product" table="t_product" discriminator-value="A"> name="id" > <discriminator column="type" type="string"></discriminator> name="price"></property> <subclass name="com.hibernate.entity.Book" discriminator-value="B"> <property name="author"></property> </subclass> name="com.hibernate.entity.Ps" discriminator-value="P"> name="handler"></property> </class> |
第二种方式
每一个类建立自己的一张表 ,表间的一对一关系用外键关联描述
Jioned-subclass 来描述子表特有的属性
table="t_product"> type="java.lang.String"> class="uuid"/> name="name" length="20"/> </property> name="price" type="java.lang.Double"></property> <joined-subclass table="t_book"> <key column="bid"></key> </joined-subclass> table="t_Ps"> column="pid"></key> |
第三种方式 将每个类的所有属性都建立一个表,(包含着父类的所有属性)
Unioned-subclass
<class <id <generator class="uuid" </id> <property name="NAME" length="20" /> </property> name="price" type="java.lang.Double"> <union-subclass <property </union-subclass> table="t_ps"> </union-subclass> </class> |
使用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 { //老师和学生实体保存 Stu s1=new Stu(); Stu s2=new Stu(); Set<Stu> ss=new HashSet<Stu>(); t.setStus(ss); //保存 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.save(p); session.save(b); session.save(p1); //将product对象 查找出来 public void query(){ Product p=(Product)session.get(Product.class, "4028d0814ec3de48014ec3de49950002"); System.out.println(p.getPrice()); Book b=(Book)p; System.out.println(b.getAuthor()); public static void main(String[] args) { CRUD crud=new CRUD(); // crud.save2(); crud.query(); } |