Hibernate映射
一对多 多对一 一对一 多对多
一对多映射
set元素的常用属性
package com.imooc.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; private static Session session; static { // 创建Configuration对象 读取hibernate.cfg.cml文件。完成初始化 Configuration config = new Configuration().configure(); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(). applySettings(config.getProperties()); StandardServiceRegistry ssr = ssrb.build(); sessionFactory = config.buildSessionFactory(ssr); } // 获取SessionFactory对象 public static SessionFactory getSessionFactory() { return sessionFactory; } // 获取Session public static Session getSession() { session = sessionFactory.openSession(); return session; } // 关闭Session public static void closeSession(Session session) { if (session != null) { session.close(); } } }
单向一对多:
班级(Grade)——>学生(Student)
班级类中有属性 set<Student>
在班级中配置
<!-- 配置单向的一对多关联关系 --> <set name="students" table="STUDENT"> <!-- 指定关联的外键 --> <key> <column name="GID" /> </key> <one-to-many class="com.imooc.entity.Student" /> </set>
package com.imooc.entity; import java.util.Set; import org.hibernate.Session; import org.hibernate.Transaction; import com.imooc.util.HibernateUtil; /** * 单向的一对多关系 * @author Administrator * */ public class Test { public static void main(String[] args) { //add(); //findStudentByGrade(); //update(); delete(); } // 将学生添加到班级 public static void add() { Grade g = new Grade("一班", "Java软件开发一班"); Student s1 = new Student("张三", "男"); Student s2 = new Student("李四", "女"); g.getStudents().add(s1); g.getStudents().add(s2); Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); session.save(g); session.save(s1); session.save(s2); tx.commit(); HibernateUtil.closeSession(session); } public static void findStudentByGrade() { Session session = HibernateUtil.getSession(); Grade grade = (Grade) session.get(Grade.class, 1); Set<Student> students = grade.getStudents(); for (Student s: students) { System.out.println(s); } } // 修改学生信息 public static void update() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); Grade g = new Grade("二班", "Java软件开发二班"); Student student = (Student) session.get(Student.class, 1); g.getStudents().add(student); session.save(g); tx.commit(); HibernateUtil.closeSession(session); } // 删除学生信息 public static void delete() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); Student student = (Student) session.get(Student.class, 2); session.delete(student); tx.commit(); HibernateUtil.closeSession(session); } }
单向多对一关联:
1、多对一的关系和关系数据库中的外键参照关系最匹配,即在己方(多方)的表中的一个外键参照另一个表的主键;
2、通过在多方持有一方的引用实现,需要在“多”的一端使用<many-to-one>配置
<!-- 配置多对一关系 --> <many-to-one name="grade" class="com.imooc.entity.Grade" column="gid" > </many-to-one>
public static void save() { Grade g = new Grade("一班", "Java软件开发一班"); Student s1 = new Student("张三", "男"); Student s2 = new Student("李四", "女"); s1.setGrade(g); s2.setGrade(g); Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); session.save(g); session.save(s1); session.save(s2); tx.commit(); HibernateUtil.closeSession(session); }
对于inverse属性的说明:
1、一方的hbm.xml文件的<set>节点的inverse属性指定了关联关系的控制方向,默认由one方来维护;
2、关联关系中,inverse="false"则为主动方,由主动方负责维护关联关系;
3、在一对多关联中,设置one方的inverse为true,这将有助于性能的改善。
cascade属性
HQL (Hibernate Query Lanaguage) Hibernate查询语言
HQL是面向对象的查询语言
HQL语句形式:select...from...where...group by...having...order by...
HQL 对Java类的属性大小写敏感,对关键字大小写不敏感
1.from查询一个对象的所有字段(Seller是一个实体类)
String hql = "from Seller"; Query query = session.createQuery(hql); List<Seller> sellers = query.list(); for (Seller seller: sellers) { System.out.println(seller); }
2.查询指定多个字段,返回的是Object[]
String hql = "select id, name from Seller"; Query query = session.createQuery(hql); List<Object[]> result = query.list(); for (Object[] objs: result) { System.out.println("id:" + objs[0] + ",name:" + objs[1]); }
3.只查询一个字段,返回的是Object
String hql = "select name from Seller"; Query query = session.createQuery(hql); List<Object> result = query.list(); for (Object obj: result) { System.out.println("name:" + obj); }
4.查询时可以使用别名,这样查询多个表的时候,可以防止属性名冲突,这里s作为Seller的别名
String hql = "select s.name from Seller s"; Query query = session.createQuery(hql); List<Object> result = query.list(); for (Object obj: result) { System.out.println("name:" + obj); }
5.list返回查询结果 通过new list()
String hql = "select new list(id,name) from Seller"; Query query = session.createQuery(hql); List<List> result = query.list(); for (List list: result) { System.out.println("id:" + list.get(0) + ",name:" + list.get(1)); }
6.以map形式返回查询结果
String hql = "select new map(id,name) from Seller"; Query query = session.createQuery(hql); List<Map> maps = query.list(); for (Map map: maps) { System.out.println("id:" + map.get("0") + ",name:" + map.get("1")); }
通过属性的别名获取,注意,如果使用别名就不可以使用下标获取了
String hql = "select new map(id as id_, name as name_) from Seller"; Query query = session.createQuery(hql); List<Map> maps = query.list(); for (Map map: maps) { System.out.println("id:" + map.get("id_") + ",name:" + map.get("name_")); }
7.以自定义类型返回查询结果
// Seller必须有由id和name组成的构造函数 String hql = "select new Seller(id,name) from Seller"; Query query = session.createQuery(hql); List<Seller> result = query.list(); for (Seller seller: result) { System.out.println("id:" + seller.getId() + ",name:" + seller.getName()); }
public class Seller implements Serializable { private Long id;// 主键 private String name;// 名称 private String tel;// 电话 ... // 其他属性 public Seller(){ } public Seller(Long id, String name) { this.id = id; this.name = name; } ... // getter & setter }
8.返回不重复的查询结果 通过distinct关键字
String hql = "select distinct sex from Customer";
条件查询
9.通过比较运算符 =, <, >, <=, >=, <>
String hql = "from Seller where id > 2";
10.判断是否为null
is null 或者 =null 判断为空
is not null 或者 <>null 判断不为空
String hql = "from Seller where name is not null";
11.范围查询
in(a,b,...) 查询值为括号中的某个值的元素 ()中可以是子查询
between a and b 查询值在[a,b]之间的元素
String hql = "from Customer where age in (20, 40)"; String hql = "from Customer where age not in (20, 40)"; String hql = "from Customer where age between 30 and 40"; String hql = "from Customer where age not between 30 and 40";
12.字符串模式匹配
通过like 关键字
通配符:% 匹配任意个字符,_ 匹配一个字符
例:查询名字两个字且姓李的人
String hql = "from Customer where name like '李_' ";
13.逻辑运算 and or
同sql……
14.集合运算
is [not] empty,集合【不】为空,不包含任何元素;对应SQL的exists运算
member of 元素属于集合 ;对应SQL的in运算。
15.四则运算
可以做select子句或者where子句中使用
例: where price*5 > 3000
16.单个查询
query.uniqueResult() 如果返回多个查询结果则报错
17.order by子句
对查询结果进行排序
select ... from ... where ... order by ... asc(desc) 默认asc
按多个属性排序,用逗号隔开
总结:和sql好像哦~