1.n+1问题:当查询多一方时,默认会把对应一的一端也查出来.
2.解决方法:
A:设置关联关系 @ManyToOne(fetch=FetchType.LAZY)
B.这种用得比较少,在一的那端设置 @BatchSize(size=5)
C.用外边接 from Topic t left join fetch t.category c
3.例子:
model:
package com.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.BatchSize;
@Entity
//@BatchSize(size=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;
}
}
package com.hibernate.model;
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;
@Entity
@NamedQueries(
{
@NamedQuery(name="topic.selectCertainTopic",query="from Topic t where t.id = :id")
}
)
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;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@ManyToOne(fetch=FetchType.LAZY)
public Category getCategory() {
return category;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public void setCategory(Category category) {
this.category = category;
}
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
}
Test:
package com.hibernate.test;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.hibernate.model.Category;
import com.hibernate.model.Msg;
import com.hibernate.model.MsgInfo;
import com.hibernate.model.Topic;
public class HQLTest {
private static SessionFactory sf = null;
@BeforeClass
public static void beforeClass() {
// new SchemaExport(new
// AnnotationConfiguration().configure()).create(false, true);
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sf.close();
}
@Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(
false, true);
testSave();
}
public static void main(String[] args) {
new HQLTest().testSchemaExport();
}
@Test
public void testSave() {
Session session = sf.openSession();
session.beginTransaction();
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();
}
//N+1
@Test
public void testQuery1() {
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());
System.out.println(t.getCategory().getName());
}
session.getTransaction().commit();
session.close();
}
//join fetch
@Test
public void testQuery4() {
Session session = sf.openSession();
session.beginTransaction();
//List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
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());
System.out.println(t.getCategory().getName());
}
session.getTransaction().commit();
session.close();
}
}