一方:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.entity.Author" table="Author" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<set name="books" inverse="true" cascade="save-update">
<key column="author_id"/>
<one-to-many class="com.entity.Book"/>
</set>
</class>
</hibernate-mapping>
多方:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.entity.Book" table="Book" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<many-to-one name="author" class="com.entity.Author" cascade="save-update">
<column name="author_id"></column>
</many-to-one>
</class>
</hibernate-mapping>
Insert操作:
从一方操作:
public void insert(){
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Author author = new Author();
author.setName("李威");
Book book = new Book();
book.setName("斗罗大陆");
book.setAuthor(author);
author.getBooks().add(book);
session.save(author);
transaction.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if(transaction!=null){
transaction.rollback();
}
}
}
从多方操作:
public void insert(){
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Author author = new Author();
author.setName("李威");
Book book = new Book();
book.setName("斗罗大陆");
book.setAuthor(author);
author.getBooks().add(book);
session.save(author);
transaction.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if(transaction!=null){
transaction.rollback();
}
}
}
明显从一方操作需要建立更复杂的联系,所以,应该从多方建立联系。
更新操作:
一方:
public void modify(){
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Book book = (Book)session.load(Book.class, 18);
Author author = (Author)session.load(Author.class, 23);
book.setAuthor(author);
session.update(book);
transaction.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if(transaction!=null){
transaction.rollback();
}
}
}
多方:
public void modify(){
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Book book = (Book)session.load(Book.class, 18);
Author author = (Author)session.load(Author.class, 24);
book.setAuthor(author);
author.getBooks().add(book);
session.update(author);
transaction.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if(transaction!=null){
transaction.rollback();
}
}
}
查询操作 :
一方:注意此时如果没有设置懒加载,获取的set是代理对象,如果session关闭后,在获取对象属性,会遇到懒加载问题,可通过设置lazy=”false”,或者设置session在获取属性后关闭解决。
public void select(){
Transaction transaction = null;
Set<Book> books = null;
try {
transaction = session.beginTransaction();
Author author = (Author)session.load(Author.class, 23);
books = author.getBooks();
for (Book book : books) {
System.out.println(book.getName());
}
transaction.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if(transaction!=null){
transaction.rollback();
}
}
}
多方:同样存在懒加载问题
public void select(){
Transaction transaction = null;
Author author = null;
try {
transaction = session.beginTransaction();
Book book = (Book)session.load(Book.class, 16);
author = book.getAuthor();
System.out.println(author.getName());
transaction.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if(transaction!=null){
transaction.rollback();
}
删除操作:
一方:如果需要级联删除多方,将cascade设置为all
public void delete(){
Transaction transaction = null;
try {
transaction = session.beginTransaction();
//Book book = (Book)session.load(Book.class, 18);
Author author = (Author)session.load(Author.class, 23);
session.delete(author);
transaction.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if(transaction!=null){
transaction.rollback();
}
}
}
多方:
public void delete(){
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Book book = (Book)session.load(Book.class, 18);
//Author author = (Author)session.load(Author.class, 23);
session.delete(book);
transaction.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if(transaction!=null){
transaction.rollback();
}
}
}