2017-02-04 3 views
-1

Mein Ziel ist es, die Liste der alle Mitarbeiter pro den ausgewählten Unternehmen zu erhalten. Wie Sie in der JSP-Datei am Ende der Frage sehen kann, erhalte ich die Fehlermeldung:java jsp erhalten Liste pro ausgewählte Element

{IndirectList: not instantiated} 

könnten Sie mir bitte helfen, die Liste aller Mitarbeiter pro ausgewählte Unternehmen zu holen?

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(); 
    } 
} 

in meinem 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); 
    } 
.... 

und in meinem 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; 
    } 
} 

und hier wähle ich den Firmennamen (ID) an die detailsCompany umgeleitet werden, um alle Mitarbeiter des Unternehmens zu sehen:

<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> 

und schließlich in meiner detailsCompany.jsp JSP-Datei, ich bin bei Verlust, wie man ziehen Sie die Liste der Mitarbeiter pro Unternehmen Ihrer Wahl.

<jsp:useBean id="company" type="java.util.List<analysis.data.Company>" scope="request"/> 

<p>Company : <%=company.getEmployees()%></p> 

Ich erhalte diese {IndirectList: nicht instanziiert} statt die Liste der Mitarbeiter. Ich bin mir nicht sicher, ob mein Code richtig ist.

+1

Ich werde den ganzen Abend die ganze Frage lesen. Hast du Mitleid mit uns? – Andremoniy

Antwort

2

Das Problem ist mit

`<%=company.getEmployees()%>` 

getEmployees() gibt eine Liste Objekt so JSP seine toString-Funktion, die in der Nachricht führt ruft "IndirectList: nicht instanziiert"

Wenn Sie den Mitarbeiter drucken möchten Details (dh Gleichwertigkeit seiner toString-Methode), können Sie so etwas wie dies zu tun, statt:

`<p>Company : <% List<Employee> employees = company.getEmployees(); 
    for(i=0;i<employees.size();i++)%> 
     <%= employees.get(i);%> 
</p> 
` 

Sie auch einige Zeilenumbrüche hinzufügen möchten s trennen Sie die verschiedenen Mitarbeiterakten.

Verwandte Themen