我在EclipseLink中遇到了一个奇怪的行为。我将@BatchFetch(value = BatchFetchType.IN)添加到这种情况下的关系到Student from Student。当我查询所有两个学生并且它正在查询相同的地址和学生两次时,我正在检查EclipseLink生成的SQL:
EclipseLink版本2.4.0
首先是没有BatchFetch的查询:
SELECT ID, NAME FROM STUDENT
SELECT ID, DESCRIPTION, STUDENT_ID FROM ADDRESS WHERE (STUDENT_ID = ?)
bind => [1]
SELECT ID, DESCRIPTION, STUDENT_ID FROM ADDRESS WHERE (STUDENT_ID = ?)
bind => [2]
预期的SQL但现在当我将BatchFetch添加到Address关系时,这就是生成的内容:
SELECT ID, NAME FROM STUDENT
SELECT ID, DESCRIPTION, STUDENT_ID FROM ADDRESS WHERE (STUDENT_ID IN (?,?))
bind => [1, 2]
SELECT ID, NAME FROM STUDENT WHERE (ID = ?)
bind => [2]
SELECT ID, DESCRIPTION, STUDENT_ID FROM ADDRESS WHERE (STUDENT_ID = ?)
bind => [2]
如果学生已经从第一个查询“SELECT ID,NAME FROM STUDENT”中获取了所有这些内容,那么为什么EclipseLink会查询该学生?为什么如果它已经从SELECT ID,DESCRIPTION,STUDENT_ID FROM ADDRESS获得它,它为什么要查询ADDRESS在哪里(STUDENT_ID IN(1,2)) 只有在添加BatchFetch批注时才会发生这种奇怪的行为。
正如您所看到的,它正在查询学生和地址两次。
SELECT ID, NAME FROM STUDENT WHERE (ID = ?)
bind => [2]
SELECT ID, DESCRIPTION, STUDENT_ID FROM ADDRESS WHERE (STUDENT_ID = ?)
bind => [2]
这些是类:
Student.java:
@Entity
public class Student implements Serializable {
private Long id;
private String name;
private List<Address> addresses = new ArrayList<Address>();
private List<Classroom> classrooms = new ArrayList<Classroom>();
public Student() {
}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQSTUDENTID")
@SequenceGenerator(name="SEQSTUDENTID", sequenceName="SEQSTUDENTID", allocationSize=1)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy="student", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@BatchFetch(value=BatchFetchType.IN)
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
@ManyToMany(mappedBy="students", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
public List<Classroom> getClassrooms() {
return classrooms;
}
public void setClassrooms(List<Classroom> classrooms) {
this.classrooms = classrooms;
}
}
Address.java:
@Entity
public class Address implements Serializable {
private Long id;
private Student student;
private String description;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQADDRESSID")
@SequenceGenerator(name="SEQADDRESSID", sequenceName="SEQADDRESSID", allocationSize=1)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/META-INF/application-context-root.xml"})
@Transactional
public class TestingFetch {
@PersistenceContext
private EntityManager entityManager;
private Student student1;
private Student student2;
@Before
public void setUp() {
List<Student> students = new ArrayList<Student>();
student1 = new Student();
student1.setName("Alfredo Osorio");
Address student1Address1 = new Address();
student1Address1.setDescription("FirstAddressStudent1");
student1Address1.setStudent(student1);
student1.getAddresses().add(student1Address1);
Address student1Address2 = new Address();
student1Address2.setDescription("SecondAddressStudent1");
student1Address2.setStudent(student1);
student1.getAddresses().add(student1Address2);
students.add(student1);
student2 = new Student();
student2.setName("Jorge Ramirez");
students.add(student2);
Address student2Address1 = new Address();
student2Address1.setDescription("FirstAddressstudent2");
student2Address1.setStudent(student2);
student2.getAddresses().add(student2Address1);
Address student2Address2 = new Address();
student2Address2.setDescription("SecondAddressstudent2");
student2Address2.setStudent(student2);
student2.getAddresses().add(student2Address2);
Classroom classroom1 = new Classroom();
classroom1.setName("Mathematics");
Classroom classroom2 = new Classroom();
classroom2.setName("Physics");
Classroom classroom3 = new Classroom();
classroom3.setName("Chemistry");
classroom1.getStudents().add(student1);
student1.getClassrooms().add(classroom1);
classroom1.getStudents().add(student2);
student2.getClassrooms().add(classroom1);
classroom2.getStudents().add(student1);
student1.getClassrooms().add(classroom2);
classroom3.getStudents().add(student2);
student2.getClassrooms().add(classroom3);
for (Student student : students) {
entityManager.persist(student);
}
entityManager.flush();
entityManager.clear();
}
@Test
public void testFetch1() {
String jpql = "select m from Student m";
Query query = entityManager.createQuery(jpql);
List<Student> list = (List<Student>)query.getResultList();
}
}
答案 0 :(得分:1)
从您的关系中删除EAGER。 EAGER导致批量查询在第一次查询完成之前执行,从而导致额外的选择。
(即,在构建第一个学生时,批处理查询选择所有地址,并且该地址有一个ManyToOne返回给学生也是EAGER,因此导致查询。