我正在与JPA合作以获得深刻见解,我指的是Pro JPA2掌握Java Persistance API。
在第6章中,我正在使用示例 使用分离的实体 , 我根据给定的指南创建了示例。 本主题说 @ ManyToOne / @ OneToMany双向 映射是延迟加载的。但是当我创建相同的示例时,这种关系是延迟加载的,这不是真的。关系急切地被扼杀。 这是我的例子
我的JSF Templete:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Student Registration</title>
</h:head>
<h:body>
<h2>Enter student Details</h2>
<h:form>
<h:commandButton actionListener="#{employeeService.saveEmployee}"
value="Save" />
<h:inputText value="#{employeeService.empId}" id="empId" />
<h:commandButton actionListener="#{employeeService.updateEmployee}"
value="update" />
</h:form>
<form method="post" action="employee">
<input type="submit" value="go" />
</form>
</h:body>
</html>
EmployeeServlet
package com.jsfapp.servlet;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jsfapp.dao.EmployeeDAO;
import com.jsfapp.jpa.entities.Department;
@WebServlet(urlPatterns = "/employee")
public class EmployeeServlet extends HttpServlet {
@EJB
private EmployeeDAO employeeService;
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*
* List<Employee> employees = employeeService.findAll();
* req.setAttribute("employees", employees);
* System.out.println("===>"+employees.get(0).getName());;
*/
List<Department> dept = employeeService.getDepartment();
req.setAttribute("departments", dept);
this.getServletContext().getRequestDispatcher("/emp.jsp")
.forward(req, resp);
}
}
EmployeeDAO
package com.jsfapp.dao;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import com.jsfapp.jpa.entities.Department;
import com.jsfapp.jpa.entities.Employee;
@Stateless(name = "employeeDAO")
@LocalBean
public class EmployeeDAO {
@PersistenceContext(unitName = "JSFApplication")
private EntityManager em;
public void saveEmployee(Employee emp) {
em.persist(emp);
}
public void updateEmployee(Employee emp) {
Employee employeeMerged = em.merge(emp);
employeeMerged.setName("AfterMerge");
}
public Employee findById(Integer empId) {
return em.find(Employee.class, empId);
}
public List<Employee> findAll() {
TypedQuery<Employee> employeeQuery = em.createQuery(
"SELECT e FROM Employee e", Employee.class);
return employeeQuery.getResultList();
}
public List<Department> getDepartment() {
TypedQuery<Department> dept = em.createQuery(
"SELECT d FROM Department d",Department.class);
//dept.setParameter("deptId", 3);
List<Department> depts = dept.getResultList();
return depts;
}
}
部门实体
package com.jsfapp.jpa.entities;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
@OneToMany(mappedBy = "department")
private List<Employee> employees;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
员工实体
package com.jsfapp.jpa.entities;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "Employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "DEPT_ID")
private Department department;
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "EMP_ADDR_ID")
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
现在,当我获得部门实体时,它会加载延迟加载的员工。我知道它依赖于提供者,但是为什么它会被急切地加载。 请帮忙
P.S。我正在使用glassfish3,Eclipse Link 2.3 for JPA