我对 EclipseLink 有一个奇怪的行为。在这种情况下,我将@BatchFetch(value=BatchFetchType.IN) 添加到来自学生的地址的关系中。当我查询所有两个学生并且它查询相同的地址和学生两次时,我正在检查 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 添加到地址关系时,会生成以下内容:
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]
如果 EclipseLink 已经从第一个查询“SELECT ID, NAME FROM STUDENT”中获得了所有学生,为什么还要查询学生,如果它已经从 SELECT ID、DESCRIPTION、STUDENT_ID FROM ADDRESS WHERE (STUDENT_ID) 中获得它,为什么还要查询 ADDRESS 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]
这些是类:
学生.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;
}
}
地址.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();
}
}