Hibernate一对多关系详解

一方:
<?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();
			}
		}
	}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值