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

Hibernate笔记③--集合映射、组合映射、联合主键、查询案例

时间:2015-07-28 10:26:54      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

lazy 懒加载 默认为proxy

?

继承映射

discriminant column="type" type="string"

?

集合映射

生成表的语句:

public class DbCreate {

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

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

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

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

????}

}

?

集合的实体

????private String id;

????private String name;

????private Set setValue;

????private List listValue;

????private String[] arrayValue;

????private Map mapValue;

?

该配置文件

<hibernate-mapping>

<class name="com.model.Coll" table="t_coll">

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

<generator class="uuid" />

</id>

?

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

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

</property>

????

<set name="setValue" table="set_table">

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

????<element type="string" column="set_value"></element>

</set>

?

<list name="listValue" table="list_table">

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

????????????<list-index column="list_index"/>

????????????<element type="string" column="list_value"/>

????????</list>

<array name="arrayValue" table="array_table">

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

????????????<list-index column="array_index"/>

????????????<element type="string" column="array_value"/>

????????</array>

?

<map name="mapValue" table="map_table">

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

????????????<map-key type="string" column="map_key"/>

????????????<element type="string" column="map_value"/>

????????</map>

?

</class>

</hibernate-mapping>

生成的表结构

技术分享

?

自己写的crud

package com.pcx.dao;

?

import java.util.ArrayList;

import java.util.HashMap;

import java.util.HashSet;

import java.util.List;

?

import org.hibernate.Hibernate;

import org.hibernate.Session;

import org.hibernate.Transaction;

?

import com.pcx.model.Collection;

import com.pcx.util.HibernateSessionFactory;

?

public class CollTest {

????public void testQueryColl(String id){

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

????????Collection c=(Collection) session.get(Collection.class, id);

????????List list=c.getListValue();

????????for (Object o : list) {

????????????System.out.println(o);

????????}

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

????}

????

????public void testColl(){

????????Collection c=new Collection();

????????c.setName("测试");

????????

????????String[]ss={"a","b","c","d"};

????????ArrayList<String>list=new ArrayList<String>();

????????list.add("a");

????????list.add("c");

????????list.add("e");

????????

????????HashSet<String>set=new HashSet<String>();

????????set.add("1");

????????set.add("3");

????????set.add("aaaa");

????????

????????HashMap<String, String>map=new HashMap<String, String>();

????????map.put("1", "11");

????????map.put("2", "22");

????????map.put("3", "33");

????????

????????c.setArrayValue(ss);

????????c.setListValue(list);

????????c.setSetValue(set);

????????c.setMapValue(map);

????????

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

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

????????session.save(c);

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

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

????}

???? ?

????

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

????????CollTest test=new CollTest();

//????????test.testColl();

????????test.testQueryColl("4028d0814ecd7aa4014ecd7aa66c0001");

????}

}

?

组合映射

两个实体类对应生成一张表格

Emp类:

private String id;

????private String name;

????private Address address;

对应的地址类:address类

????private String homeAddress;

????private String comAddress;

?

对应的配置文件

<hibernate-mapping>

<class name="com.pcx.model.Emp" table="t_emp">

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

<generator class="uuid" />

</id>

?

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

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

</property>

???? ?

????<component name="address">

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

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

????</component>

</class>

</hibernate-mapping>

对应生成的表结构为

技术分享

自己写的crud

package com.pcx.dao;

?

import org.hibernate.Session;

import org.hibernate.Transaction;

?

import com.pcx.model.Address;

import com.pcx.model.Emp;

import com.pcx.util.HibernateSessionFactory;

?

public class ComponentTest {

????public void testCom(){

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

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

????????Address ad=new Address();

????????ad.setComAddress("济南长清");

????????ad.setHomeAddress("山东泰安");

????????Emp e=new Emp();

????????e.setName("程志");

????????e.setAddress(ad);

????????session.save(e);

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

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

????}

????

????public void testQueryCom(String id){

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

????????Emp e=(Emp) session.get(Emp.class,id );

????????System.out.println(e.getId());

????????System.out.println(e.getName());

????????System.out.println(e.getAddress().getHomeAddress());

????}

????

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

????????ComponentTest com=new ComponentTest();

//????????com.testCom();

????????com.testQueryCom("4028d0814ece0647014ece0648f20001");

????}

}

?

?

?

?

联合主键

如果一个表没主键,那么由表生成类的时候会单独生成一个 id类

技术分享

Student 类中就单独一个StudentId

StudentId中则包含 id name

生成的配置文件为:

<?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">

<!--

Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

<class name="com.pcx.model.Student" table="student" catalog="hibernate">

<composite-id name="id" class="com.pcx.model.StudentId">

<key-property name="id" type="java.lang.Integer">

<column name="id" />

</key-property>

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

<column name="name" />

</key-property>

</composite-id>

</class>

</hibernate-mapping>

?

在一个例子

Emp

private PKId pkid;

????private String name;

PKId

private Integer fristId;

????private Integer lastId;

?

配置文件:

<?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.pcx.entity.Emp" table="t_emp">

<composite-id name="pkid">

????<key-property name="fristId" />

????<key-property name="lastId" />

</composite-id>

?

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

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

</property>

???? ?

</class>

</hibernate-mapping>

配置文件字段配置错了可能会出现

类的变量没配对

提示错误找不到配置文件。

?

?

?

多对多关系

多个学生对应多个老师

学生

private String id;

????private String name;

????private Set<Teacher> teachers;

老师

????private String id;

????private String name;

????private Set<Student> students;

配置文件

学生

<hibernate-mapping>

<class name="com.pcx.entity.Student" table="t_student" >

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

<generator class="uuid" />

</id>

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

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

</property>

?

<set name="teachers" table="t_teacher_student" cascade="all">

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

????<many-to-many class="com.pcx.entity.Teacher" column="tid" ></many-to-many>

</set>

</class>

</hibernate-mapping>

老师

<hibernate-mapping>

<class name="com.pcx.entity.Teacher" table="t_teacher">

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

<generator class="uuid" />

</id>

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

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

</property>

?

<set name="students" table="t_teacher_student" cascade="all">

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

????<many-to-many class="com.pcx.entity.Student" column="sid"></many-to-many>

</set>

</class>

</hibernate-mapping>

?

对应的crud

package com.pcx.dao;

?

import java.util.HashSet;

import java.util.Set;

?

import org.hibernate.Session;

import org.hibernate.Transaction;

?

import com.pcx.entity.Student;

import com.pcx.entity.Teacher;

import com.pcx.util.HibernateSessionFactory;

?

public class TestManyToMany {

????private void testManyToMany(){

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

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

????????

????????Teacher t1=new Teacher();

????????t1.setName("");

????????Teacher t2=new Teacher();

????????t2.setName("");

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

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

????????Student s3=new Student();

????????Student s4=new Student();

????????

????????s1.setName("aaa");

????????s2.setName("aaa");

????????s3.setName("bbb");

????????s4.setName("aaa");

????????

????????Set<Teacher>set=new HashSet<Teacher>();

????????set.add(t1);set.add(t2);

????????s1.setTeachers(set);

????????

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

????????

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

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

????}

????public void testQuery(String id){

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

????????Teacher t=(Teacher) session.get(Teacher.class, id);

????????System.out.println(t.getName());

????????Set<Student>ss=t.getStudents();

????????for (Student student : ss) {

????????????System.out.println(student.getName());

????????}

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

????}

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

????????TestManyToMany test=new TestManyToMany();

//????????test.testManyToMany();

????????test.testQuery("4028d0814ece5103014ece51053b0002");

????}

}

?

一对一

?

查询

?

Hql

?

懒加载的问题

一对多的表关系中 默认是proxy

true 不加载关联的 老师查出来学生不被加载了

false 加载关联的

enchor代理

?

load验证id在表中是否存在, 不加载其他的属性,若不关闭Session 可以取出name 因为被代理掉了

同Query类的iterator

?

查询一个单体的时候

根据ID来查询 get load

get直接发送sql语句,直接将数据从数据库中加载到当前程序中,并且置入对象属性。

load不发送sql语句,只是查询当前给定的id在数据库中是否有这样一条记录对应存在

协助维护数据关系的

?

可以通过forward --过滤器 用于开启或关闭Session

?

?

unionQueresult()

?

分页

?

Criteria类

gt 大于1000的

lt 小于1000的

复合条件

?

.add().add()

排序 addorder

ReStrictions

?

?

结合离线查询

查询的案例代码

package com.pcx.dao;

?

import java.util.List;

?

import org.hibernate.Criteria;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.Transaction;

import org.hibernate.criterion.DetachedCriteria;

import org.hibernate.criterion.Order;

import org.hibernate.criterion.Restrictions;

?

?

import com.pcx.model1.Emp;

import com.pcx.util.HibernateSessionFactory;

?

public class QueryTest {

????

????public static void initDate(){

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

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

????????for (int i = 0; i < 100; i++) {

????????????Emp e=new Emp();

????????????e.setName("张三"+i);

????????????session.save(e);

????????}

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

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

????}

????public static List<Emp> hql1(){

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

????????

????????String hql=" from Emp e where e.sal>1000";

????????Query query=session.createQuery(hql);

????????

????????List<Emp>emps=query.list();

????????for (Emp emp : emps) {

????????????System.out.println(emp);

????????}

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

????????return emps;

????}

????/**

???? * 查询的第二种情况

???? * @param args

???? */

????public static void hql2(){

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

????????

????????String hql="select name,age from Emp ";

????????Query query=session.createQuery(hql);

????????

????????List emps=query.list();

????????

????????Object[]obj=(Object[]) emps.get(0);

????????

????????String name=(String) obj[0];

????????Integer age=(Integer) obj[1];

????????System.out.println("name:"+name+" age:"+age);

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

????}

????/**

???? * 绑定变量

???? * @param args

???? */

????public static void bindVar(){

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

????????

????????String hql="from Emp where name=:name";

????????Query q=session.createQuery(hql);

????????q.setParameter("name", "张三");

????????List<Emp>list=q.list();

????????System.out.println(????list.get(0));;

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

????}

//????使用Criteria

????public static void load1(){

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

????????Criteria c=s.createCriteria(Emp.class);

????????//这个Criteria绝大多数的应用是增加条件

//????????c.add(Restrictions.eq("name", "李四"));

//????????c.add(Restrictions.between("sal", 1000.00, 5000.00));

//????????c.add(Restrictions.gt("age", 20));//大于

//????????c.add(Restrictions.lt("age", 20));//小于

//????????c.add(null).add(null);//可以不停的去add 节省了拼sql的时间

//????????c.add(Restrictions.like("name", "%"));

//????????c.setFirstResult(0);

//????????c.setMaxResults(20);

????????c.addOrder(Order.asc("sal"));

????????List<Emp>list=c.list();

????????for (Emp emp : list) {

????????????System.out.println(emp);

????????}

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

????}

????//离线查询 脱离Session 创建查询

????public static void load2(){

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

????????

????????DetachedCriteria dc=DetachedCriteria.forClass(Emp.class);

????????dc.add(Restrictions.eq("name", "李四"));

????????List<Emp>list=dc.getExecutableCriteria(session).list();

????????for (Emp emp : list) {

????????????System.out.println(emp);

????????}

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

????}

????

????//分页查询

????public static void load3(int firstRow,int maxResult){

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

????????Query query=session.createQuery("from Emp");

????????query.setFirstResult(firstRow);

????????query.setMaxResults(maxResult);

????????List<Emp>list=query.list();

????????for (Emp emp : list) {

????????????System.out.println(emp);

????????}

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

????}

????//分页查询二

????public static void load4(int currentPage){

????????int pageSize=10;

????????int totalPageSize=(int)getTotalSize();

????????int pageCount=totalPageSize/pageSize;

????????if (totalPageSize%pageSize!=0) {

????????????pageCount++;

????????}

????????System.out.println(""+pageCount+"");

????????System.out.println(""+totalPageSize+"条记录");

????????

????????//如果客户给出的 当前页 小于 1 则给当前页赋值1 防止负数的出现

????????if (currentPage<1) {

????????????currentPage=1;

????????}

????????

????????//如果客户给出的当前页大于总页数。则给当前页赋值为最大页数

????????if(currentPage>pageCount){

????????????currentPage = pageCount;

????????}

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

????????String hql = "from Emp order by id";

????????Query query = session.createQuery(hql);

????????query.setFirstResult((currentPage-1)*pageSize);

????????query.setMaxResults(pageSize);

????????List<Emp> list = query.list();

????????

????????for (Emp emp : list) {

????????????System.out.println(emp.getId());

????????????System.out.println(emp.getName());

????????}

????????

????????

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

?

????}

????private static long getTotalSize() {

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

????????String hql ="select count(*) from Emp";

????????Query query = session.createQuery(hql);

????????List list = query.list();

????????Object obj = list.get(0);

????????return (Long) obj;

?

????}

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

//????????List<Emp>emps=hql1();

????????//Session已经关闭,加载不了对象 如果开启懒加载的话 就不会报错

//????????for (Emp emp : emps) {

//????????????System.out.println(emp);

//????????}

????????

//????????hql2();

//????????bindVar();

//????????load2();

????????initDate();

????}

}

Hibernate笔记③--集合映射、组合映射、联合主键、查询案例

标签:

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

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