我的目标是获取所选公司的所有员工的列表。 正如您在问题末尾的jsp文件中看到的那样,我收到错误:
{IndirectList: not instantiated}
您能否帮助我获取所选公司所有员工的名单?
Company.java
package analysis.data;
import java.io.Serializable;
import java.lang.Integer;
import java.lang.String;
import java.util.List;
import javax.persistence.*;
/**
* Entity implementation class for Entity: Company
*
*/
@Entity
public class Company implements Serializable {
@Id
@GeneratedValue
private Integer id;
@Column(unique=true, nullable=false)
private String lastname;
@OneToMany(mappedBy="company", fetch=FetchType.LAZY) // LAZY = fetch when needed, EAGER = fetch immediately
private List<Employee> employees;
private static final long serialVersionUID = 1L;
public Company() {
super();
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastname() {
return this.lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname.toUpperCase();
}
public List<Employee> getEmployees() {
return this.employees;
}
}
Employee.java
package analysis.data;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
* Entity implementation class for Entity: Company
*
*/
@Entity
public class Employee implements Serializable {
@Id
@GeneratedValue
private Integer id;
@Column(nullable=false)
private String firstname;
@Column(nullable=false)
private String lastname;
@ManyToOne
private Company company;
private static final long serialVersionUID = 1L;
public Employee() {
super();
//nbAbsences = 0;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstname() {
return this.firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return this.lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Company getCompany() {
return this.company;
}
public void setCompany(Company company) {
this.company = company;
if (!company.getEmployees().contains(this)) {
company.getEmployees().add(this);
}
}
@Override
public String toString() {
return "[" + this.getId() + "] " + this.getFirstname() + " " + this.getLastname();
}
}
在我的controller.java里面
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
....
} else if (methode.equals("get") && action.equals("/detailsCompany")) {
doDetailsCompany(request, response);
....
private void doDetailsCompany(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.valueOf(request.getParameter("id"));
Company company = CompanyDAO.retrieveById(id);
request.setAttribute("company", company);
loadJSP(urlDetailsCompany, request, response);
}
....
并在我的CompanyDAO.java内部
public class CompanyDAO {
...
public static Company retrieveById(int id) {
EntityManager em = GestionFactory.factory.createEntityManager();
Company company = em.find(Company.class, id);
em.close();
return company;
}
...
public static List<Company> getAll() {
EntityManager em = GestionFactory.factory.createEntityManager();
Query q = em.createQuery("SELECT c FROM Company c");
@SuppressWarnings("unchecked")
List<Company> listCompany = q.getResultList();
return listCompany;
}
}
在这里我选择公司名称(id)重定向到detailsCompany以查看该公司的所有员工:
<jsp:useBean id="companys" type="java.util.List<analysis.data.Company>" scope="request"/>
<table>
<tr>
<th>Company Name</th>
<th>Number of employees</th>
<th>Employees</th>
</tr>
<% for (Company company : companys) {%>
<tr>
<td><%=company.getNom()%></td>
<td><%=company.getEmployees().size()%></td>
<td><a href="<%= getServletContext().getContextPath()%>/do/detailsCompany?id=<%=company.getId()%>">See the list</a></td>
</tr>
<% } %>
</table>
最后在我的detailsCompany.jsp
JSP文件中,我对如何提取每个公司的员工名单感到茫然。
<jsp:useBean id="company" type="java.util.List<analysis.data.Company>" scope="request"/>
<p>Company : <%=company.getEmployees()%></p>
我明白了 {IndirectList:未实例化} 而不是员工名单。 我不确定我的代码是否正确。
答案 0 :(得分:2)
问题在于
`<%=company.getEmployees()%>`
getEmployees()返回一个List对象,以便JSP调用其toString函数,从而产生消息“IndirectList:not instantiated”
如果您希望打印员工详细信息(即其toString方法的等效性),您可能需要执行以下操作:
`<p>Company : <% List<Employee> employees = company.getEmployees();
for(i=0;i<employees.size();i++)%>
<%= employees.get(i);%>
</p>
`
您可能还想添加一些换行符来分隔各种员工记录。