mysql-SpringDataJPA的一对多和多对一级联查找

SpringDataJPA的一对多和多对一级联查找

准备工作

准备工作包括spring配置文件的准备,entity实体类准备和接口的准备。
配置文件请查看我之前发的文章,这里不贴出占用页面位置,点击这里查看配置文件

entity

@Entity
@Table(name="e_reader")
public class Reader {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	private String name;
	private String password;
	@OneToMany(fetch=FetchType.EAGER)
	@JoinColumn(name="reader_id")
	private Set<Book> bookSet=new HashSet<Book>();
	@Override
	public String toString() {
		return "Reader [id=" + id + ", name=" + name + ", password=" + password + ", bookSet=" + bookSet + "]";
	}
}


@Entity
@Table(name="e_book")
public class Book{
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	@Column(name="book_name",unique=true,nullable=false,length=20)
	private String name;
	@Column(name="book_author",nullable=false)
	private String author;
	@Column(name="book_storage")
	private int storage;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="reader_id")
	private Reader reader;

	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", author=" + author + ", storage=" + storage + ", reader="
				+ "]";
	}
}

dao层

public interface ReaderDao extends Repository<Reader, Integer>{
	Reader getById(int id);
}
public interface IBooksDao extends Repository<Book, Integer>{
	List<Book> getByReader_Name(String readerName);
}

Test

public class Test1 {
	private ApplicationContext context=null;
	private IBooksDao bookDao =null;
	private ReaderDao readerDao=null;
	@Before
	public void inite() {
		context=new ClassPathXmlApplicationContext("applicationContext.xml");
		bookDao = context.getBean(IBooksDao.class);
		readerDao=context.getBean(ReaderDao.class);
	}
	
	@Test
	public void testBook() {
		 List<Book> list = bookDao.getByReader_Name("zhang");
		System.out.println(list);
	}
	
	@Test
	public void testReader() {
		Reader reader = readerDao.getById(1);
		System.out.println(reader);
	}
}

级联查找的研究

多对一

实体类Book

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="reader_id")
	private Reader reader;

后台查看代码为

  select
        book0_.id as id1_0_,
        book0_.book_author as book_aut2_0_,
        book0_.book_name as book_nam3_0_,
        book0_.reader_id as reader_i5_0_,
        book0_.book_storage as book_sto4_0_ 
    from
        e_book book0_ 
    left outer join
        e_reader reader1_ 
            on book0_.reader_id=reader1_.id 
    where
        reader1_.name=?

此时查询为懒加载,所以不会查询reader,
如果去掉懒加载,立即查询的代码为

Hibernate: 
    select
        book0_.id as id1_0_,
        book0_.book_author as book_aut2_0_,
        book0_.book_name as book_nam3_0_,
        book0_.reader_id as reader_i5_0_,
        book0_.book_storage as book_sto4_0_ 
    from
        e_book book0_ 
    left outer join
        e_reader reader1_ 
            on book0_.reader_id=reader1_.id 
    where
        reader1_.name=?
Hibernate: 
    select
        reader0_.id as id1_1_0_,
        reader0_.name as name2_1_0_,
        reader0_.password as password3_1_0_ 
    from
        e_reader reader0_ 
    where
        reader0_.id=?

此时会在左外查询结束后,级联查询reader

由此可以看出,多对一默认加载的方式为立即加载

一对多

实体类

	@OneToMany(fetch=FetchType.EAGER)
	@JoinColumn(name="reader_id")
	private Set<Book> bookSet=new HashSet<Book>();
Hibernate: 
    select
        reader0_.id as id1_1_,
        reader0_.name as name2_1_,
        reader0_.password as password3_1_ 
    from
        e_reader reader0_ 
    where
        reader0_.id=?
Hibernate: 
    select
        bookset0_.reader_id as reader_i5_1_0_,
        bookset0_.id as id1_0_0_,
        bookset0_.id as id1_0_1_,
        bookset0_.book_author as book_aut2_0_1_,
        bookset0_.book_name as book_nam3_0_1_,
        bookset0_.reader_id as reader_i5_0_1_,
        bookset0_.book_storage as book_sto4_0_1_ 
    from
        e_book bookset0_ 
    where
        bookset0_.reader_id=?

去除加载的方式后,后台代码为

Hibernate: 
    select
        reader0_.id as id1_1_,
        reader0_.name as name2_1_,
        reader0_.password as password3_1_ 
    from
        e_reader reader0_ 
    where
        reader0_.id=?

由此可见,一对多默认的加载方式是懒加载

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值