准备工作包括spring配置文件的准备,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="
+ "]";
}
}
public interface ReaderDao extends Repository<Reader, Integer>{
Reader getById(int id);
}
public interface IBooksDao extends Repository<Book, Integer>{
List<Book> getByReader_Name(String readerName);
}
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=?
由此可见,一对多默认的加载方式是懒加载