20,21,22-Hibernate容器映射技术(Set、List、Map) -mldn学习笔记 -hxzon
1、数据库并未设置外键关联,但是配置文件设置了关联。那么在执行hql语句时,不会级联删除。但是用session.delete(model)时会级联删除。
Hibernate提供的delete方法最大的缺点就是必须先把数据级联查询出来之后再删除,才能级联删除。最好的做法是用hql按id进行删除。这时要在数据库中设置外键关联。
2、本例中只有一个主表Person生成model类,
从表不生成model类。本例从表没有主键。数据库中不可能有完全相同的两行记录,所以List例子中数据库还有一个索引列posit作为区分。
=============================
一、1、Set数据库创建表
CREATE TABLE person
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL ,
name VARCHAR(20)
) ;
CREATE TABLE emails
(
id INT ,
address VARCHAR(50) ,
foreign key (id) references person(id) on delete cascade
) ;
2、一个人拥有多个不同的Email地址。private Set emails ;
<hibernate-mapping>
<class name="org.lxh.hibernate.demo01.pojo.Person" table="person">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<set name="emails" table="emails" lazy="false">
<key column="id"></key>
<element type="java.lang.String" column="address"></element>
</set>
</class>
</hibernate-mapping>
3、package org.lxh.hibernate.demo01.oper;
import java.util.List;
import java.util.TreeSet;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.lxh.hibernate.demo01.pojo.Person;
public class PersonOperate {
private Session session;
public PersonOperate() {
this.session = new Configuration().configure().buildSessionFactory()
.openSession();
}
public void insert(Person per) {
this.session.save(per);
this.session.beginTransaction().commit();
}
public Person queryById(int id) {
Person p = null;
String hql = "FROM Person AS p where p.id=?";
Query q = this.session.createQuery(hql);
q.setInteger(0, id);
List all = q.list();
if (all.size() > 0) {
p = (Person) all.get(0);
}
return p;
}
// 进行更新
public void update(Person per) {
this.session.update(per);
this.session.beginTransaction().commit();
}
// 使用HQL删除
public void delete(int id) {
String hql = "DELETE FROM Person WHERE id=?";
Query q = this.session.createQuery(hql);
q.setInteger(0, id);
q.executeUpdate();
this.session.beginTransaction().commit();
}
public void delete(Person per)
{
this.session.delete(per) ;
this.session.beginTransaction().commit() ;
}
public static void main(String args[]) {
PersonOperate po = new PersonOperate();
// Person p = new Person();
// // 设置人的姓名
// p.setName("LiXingHua");
// p.setEmails(new TreeSet());
// p.getEmails().add("
[email protected]");
// p.getEmails().add("
[email protected]");
// p.getEmails().add("
[email protected]");//可以看见数据库中只有一条记录,不会重复
// p.getEmails().add("
[email protected]");
// po.insert(p);
// Person p = po.queryById(3);
//System.out.println(p.getName());
//System.out.println(p.getEmails());
// p.getEmails().add("
[email protected]");
// p.getEmails().add("
[email protected]");
// po.update(p);
po.delete(2) ;
}
}
=====================================
二、1、List数据库创建表
CREATE TABLE person
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL ,
name VARCHAR(20)
) ;
CREATE TABLE books
(
id INT ,
posit int ,
title VARCHAR(30) ,
foreign key (id) references person(id) on delete cascade
) ;
2、一个人有多本书,书可以重复。private List books ;
<hibernate-mapping>
<class name="org.lxh.hibernate.demo02.pojo.Person" table="person">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<list name="books" table="books">
<key column="id"></key>
<index column="posit"></index>
<element type="java.lang.String" column="title"></element>
</list>
</class>
</hibernate-mapping>
posit用来区分两本相同的书。应用中不需我们关注。
3、package org.lxh.hibernate.demo02.oper;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.lxh.hibernate.demo02.pojo.Person;
public class PersonOperate {
private Session session;
public PersonOperate() {
this.session = new Configuration().configure().buildSessionFactory()
.openSession();
}
// 添加人员
public void insert(Person per) {
this.session.save(per);
this.session.beginTransaction().commit();
}
// 根据ID查询人员信息
public Person queryById(int id) {
Person p = null;
String hql = "FROM Person AS p where p.id=?";
Query q = this.session.createQuery(hql);
q.setInteger(0, id);
List all = q.list();
if (all.size() > 0) {
p = (Person) all.get(0);
}
return p;
}
public void update(Person per)
{
this.session.update(per) ;
this.session.beginTransaction().commit() ;
}
/**
* @param args
*/
public static void main(String[] args) {
PersonOperate po = new PersonOperate();
// Person p = new Person();
// p.setName("LiXingHua");
// p.setBooks(new ArrayList());
// p.getBooks().add("Java 核心技术");
// p.getBooks().add("Java 核心技术");
// p.getBooks().add("Java 核心技术");
// p.getBooks().add("Java 核心技术");
// p.getBooks().add("Java 核心技术");
// p.getBooks().add("Oracle PL SQL编程");
// p.getBooks().add("WebSphere 开发指南");
//
// po.insert(p);
Person p = po.queryById(2);
// System.out.println("姓名:" + p.getName());
// Iterator iter = p.getBooks().iterator() ;
// while(iter.hasNext())
// {
// System.out.println(" |- "+iter.next()) ;
// }
p.getBooks().add("JSP 核心技术") ;
p.getBooks().add("JSP 核心技术") ;
p.getBooks().add("JSP 核心技术") ;
po.update(p) ;
}
}
=============================
三、1、Map数据库创建表
CREATE TABLE person
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL ,
name VARCHAR(20)
) ;
CREATE TABLE phone
(
id INT ,
name VARCHAR(20) ,
numbers VARCHAR(30),
foreign key (id) references person(id) on delete cascade
) ;
2、一个人有一个电话本,电话本里一个人名对应一个电话号码。private Map phone ;
<hibernate-mapping>
<class name="org.lxh.hibernate.demo03.pojo.Person" table="person">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<map name="phone" table="phone">
<key column="id"></key>
<index type="java.lang.String" column="name"></index>
<element type="java.lang.String" column="numbers"></element>
</map>
</class>
</hibernate-mapping>
3、package org.lxh.hibernate.demo03.oper;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.lxh.hibernate.demo03.pojo.Person;
public class PersonOperate {
private Session session;
public PersonOperate() {
this.session = new Configuration().configure().buildSessionFactory()
.openSession();
}
public void insert(Person per) {
this.session.save(per);
this.session.beginTransaction().commit();
}
public Person queryById(int id) {
Person per = null;
String hql = "FROM Person AS p where p.id=?";
Query q = this.session.createQuery(hql);
q.setInteger(0, id);
List all = q.list();
if (all.size() > 0) {
per = (Person) all.get(0);
}
return per;
}
public void delete(int id)
{
String hql = "DELETE FROM Person WHERE id=?" ;
Query q = this.session.createQuery(hql) ;
q.setInteger(0, id) ;
q.executeUpdate() ;
this.session.beginTransaction().commit() ;
}
/**
* @param args
*/
public static void main(String[] args) {
PersonOperate po = new PersonOperate();
// Person p = new Person();
// p.setName("LiXingHua");
// p.setPhone(new HashMap());
// p.getPhone().put("zhangsan", "123456)");
// p.getPhone().put("lisi", "234567");
// p.getPhone().put("mldn", "01051283346");
// po.insert(p);
// Person p = po.queryById(1);
// System.out.println(p.getName());
// Set s = p.getPhone().entrySet();
// Iterator iter = s.iterator();
// while (iter.hasNext()) {
// Map.Entry me = (Map.Entry) iter.next();
// System.out.println(me.getKey() + " --> " + me.getValue());
// }
po.delete(1) ;
}
}