Hibernate 学习记录 4
来源:互联网 发布:node js 教程 编辑:程序博客网 时间:2024/05/20 00:48
这篇,我们将处理一对多的关系。
public class Book extends BaseEntity { private String name; public Book() { } public Book(String name, Person person) { this.name = name; this.person = person; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToOne @JoinColumn(name = "personId") private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; }}
public class Person extends BaseEntity { private String name; public Person() { } public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToMany(mappedBy = "person", fetch = FetchType.EAGER) private List<Book> bookList; public List<Book> getBookList() { return bookList; } public void setBookList(List<Book> bookList) { this.bookList = bookList; }}
这里同样用到了 mappedBy,
记得之前的提醒吗,
这意味 setBookList 方法不会有实质性的作用。
FetchType 指的是是否用懒加载,
即调用 getBookList 方法 时才进一步查询,这样就分散了查询的压力。
但这里会报错,因为再查询时 session 已经关闭,所以先用 EAGER 吧。
public class Main { public static void main(final String[] args) throws Exception { Person li = getLi(); System.out.print(li.getBookList().size()); HibernateUtil.close(); } private static Person getLi() { return PersonDao.getPersonById("85d78641-924c-48d3-bfdd-203fa7f7bc9d"); }}
0 0
- Hibernate 学习记录 4
- Hibernate学习记录4 缓存机制
- Hibernate学习记录
- hibernate学习记录
- hibernate缓存学习记录
- hibernate学习记录1
- hibernate学习记录2
- hibernate学习记录3
- hibernate学习记录5
- hibernate学习记录6
- hibernate学习记录7
- hibernate学习记录8
- Hibernate 学习记录
- Hibernate 学习记录 2
- Hibernate 学习记录 3
- Hibernate学习记录1
- Hibernate学习记录2
- 关于Hibernate学习记录
- 看看别人是怎么写程序的(程序员的自我修养)
- 楼教主的ACM心路历程
- UVa 1368 - DNA Consensus String
- 关于UITabbarItem在iOS 8 上显示不知道哪里来的蓝色并且模糊的问题解决办法
- 转曾钦松ACM退役文章
- Hibernate 学习记录 4
- VirtualBox上安装Linux
- tomcat myeclipse相关问题bug汇总
- vbox设置
- 弱校ACM奋斗史
- 简单的自由落体
- Your content must have a ListView whose id attribute is 'android.R.id.list' 【Android 报错】
- QT学习之路:从入门到精通_博客专题_51CTO技术博客
- Andriod Atom x86模拟器启动报错。