hibernate查询多表数据库(下)在之前的基础上继续完善

hibernate查询多表数据库(2)在之前的基础上继续完善,不懂之处翻看上章。

以前就建立好数据库的表student,这里我们要从关联关系下手,书本与学生的关系,相对于学生,一个学生可以有多本书籍,

多本书籍是一个学生的,

这里建T_book 数据库表,sid是student的id,这里不需要建外键,利用hql语句自动生成。

1、之前的工作都有,只要补充缺少的部分。在hibernate.cfg.xml文件添加这

2、创建Book实体类,需要注意它与学生的关系连接,书本为多方

package com.zhiyou100.entity;
/*
 * 验证主键identity的生成方式
 * <one-to-many>
 * many方
 */

import java.util.HashSet;
import java.util.Set;

public class Book {
//可以不用写外键的属性
	private int id;
	private String name;
	private Student student;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
}

同时,修改Student的类

package com.zhiyou100.entity;
import java.util.HashSet;
import java.util.Set;

/*
 *验证主键identity的生成方式
 *<one-to-many>
 */
public class Student {
	private int id;
	private String name;
	Set<Book> books=new HashSet<Book>();
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	 
	public Set<Book> getBooks() {
		return books;
	}
	public void setBooks(Set<Book> books) {
		this.books = books;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
	
}

3.修改Student.hbm.xml,这里与之前不同,设置了set,one-to-many标签

  <hibernate-mapping>
      <class name="com.zhiyou100.entity.Student" table="T_student">
       <id name="id" column="id" type="int">
       <generator class="identity"></generator>
       </id>
      <property name="name" column="name" type="string"></property>
      <!-- set声明Student中的属性为books -->
      <set name="books">
      <key column="sid"></key>
      <!--one-to-many:一对多  class=多方  -->
      <one-to-many class="com.zhiyou100.entity.Book"/>
      </set>
      </class>
    </hibernate-mapping>

创建Book.hbm.xml文件

 <hibernate-mapping>
      <class name="com.zhiyou100.entity.Book" table="T_book">
       <id name="id" column="id" type="int">
       <generator class="identity"></generator>
       </id>
      <property name="name" column="name" type="string"></property>
      <!-- many-to-one 多对一  name:一方的属性名字 -->
      <!--class: 一方的类 -->
      <many-to-one name="student"   column="sid"   class="com.zhiyou100.entity.Student"></many-to-one>
      <!-- student是book里面的student   column="sid" 是外键 -->
      </class>
    </hibernate-mapping>

四、测试多表操作是否成功

public class TestCase {
 public static  Session   openSession() {
		//读取数据库信息
		Configuration cfg = new  Configuration();
		cfg.configure("Hibernate.cfg.xml");
		//获取session对象
		SessionFactory sf = cfg.buildSessionFactory();
		Session session = sf.openSession();
		return session;
 }

@Test
public void testOne2Many() {
	Session s = openSession();
	Student student = (Student) s.get(Student.class, 1);
	System.out.println(student);
	Set<Book> books = student.getBooks();
	for (Book book : books) {
		System.out.println(book.getName()+"   "+book.getId());
	}
	s.close();
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值