Моя цель - получить список всех сотрудников для выбранной компании. Как вы можете увидеть в файле JSP в конце вопроса, я получаю ошибку:java 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: не инстанцированный} вместо списка сотрудников. Я не уверен, правильно ли работает мой код.
Я буду читать весь вопрос весь вечер. Вы сожалеете нас? – Andremoniy