package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Category { private int id; private String name; @Id @GeneratedValue 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; } }
package com.bjsxt.hibernate; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToMany; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity public class Topic { private int id; private String title; private Category category; private Date createDate; private List<Msg> msgs = new ArrayList<Msg>(); @OneToMany(mappedBy="topic") public List<Msg> getMsgs() { return msgs; } public void setMsgs(List<Msg> msgs) { this.msgs = msgs; } @Temporal(TemporalType.TIME) public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } @ManyToOne public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
@Test public void testSave() { Session session = sf.openSession(); session.beginTransaction(); //每一个版块ci下面有一个话题ti for(int i=0; i<10; i++) { Category c = new Category(); c.setName("c" + i); Topic t=new Topic(); t.setCategory(c); t.setTitle("t"+i); t.setCreateDate(new Date()); session.save(c); session.save(t); } session.getTransaction().commit(); session.close(); }
@Test public void testOneAddNProblem1(){ Session session = sf.openSession(); session.beginTransaction(); List<Topic> topics=(List<Topic>)session.createQuery("from Topic").list(); for(Topic t:topics){ System.out.println(t.getId()+"-"+t.getTitle()); } session.getTransaction().commit(); session.close(); }先取出Topic的所有信息,结果:
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import org.hibernate.annotations.BatchSize; @Entity @BatchSize(size=5)//当你去加载Category的时候一次性加载5条 public class Category { private int id; private String name; @Id @GeneratedValue 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; } }
@Test public void testOneAddNProblem1(){ Session session = sf.openSession(); session.beginTransaction(); List<Topic> topics=(List<Topic>)session.createQuery("from Topic").list(); for(Topic t:topics){ System.out.println(t.getId()+"-"+t.getTitle()); } session.getTransaction().commit(); session.close(); }测试结果:
@Test public void testOneAddNProblem2(){ Session session = sf.openSession(); session.beginTransaction(); List<Topic> topics=(List<Topic>)session.createQuery("from Topic t left join fetch t.category c").list(); for(Topic t:topics){ System.out.println(t.getId()+"-"+t.getTitle()); } session.getTransaction().commit(); session.close(); }测试结果:
只发出了一条sql语句,原因是用了"Topic t left join fetch",做了一个外连接,做了一个外连接的话就没有必要每拿出一条来单独再去取了。
转载请注明出处:http://blog.csdn.net/acmman/article/details/43937551