2012-05-29 41 views
0

我正在製作一個使用JBOSS和Seam的Web應用程序,但我試圖在我的一個類中使用entityManager,但它是空的。我將它連接到外部數據庫並在類中打印出entityManager,它只是表示爲空。當我嘗試打開使用類的網頁時,我得到一個空指針異常,並且網頁上說無法實例化Seam組件。我花了數小時在互聯網上試圖弄清楚什麼是錯,但我一直沒有成功。任何幫助,將不勝感激。這裏是該類的代碼:EntityManager爲空。使用Seam和JBOSS

package edu.uwrf.iss.flowershop.session; 

import java.util.ArrayList; 
import java.util.List; 

import javax.ejb.Remove; 
import javax.ejb.Stateful; 
import javax.persistence.EntityManager; 
import javax.persistence.Query; 

import org.jboss.aop.util.logging.SystemOutLoggerPlugin; 
import org.jboss.seam.ScopeType; 
import org.jboss.seam.annotations.Destroy; 
import org.jboss.seam.annotations.In; 
import org.jboss.seam.annotations.Name; 
import org.jboss.seam.annotations.Scope; 

import edu.uwrf.iss.flowershop.entity.FlowerStoreEmployee; 



@Stateful 
@Scope(ScopeType.SESSION) 
@Name("employeePort") 
public class EmployeeBean implements EmployeePortal { 

//These are the variables to store the employee information 
String empID; 
String first; 
String last; 
String ssn; 
String phone; 
String pay; 
String vehicle; 
String house; 
String street; 
String city; 
String state; 
String zip; 

@In (create=true) 
private EntityManager entityManager; 
//this is the employee list 
List<FlowerStoreEmployee> employeeList; 

//Constructor 
public EmployeeBean(){ 
    employeeList = new ArrayList<FlowerStoreEmployee>(); 
    loadEmployeeList(); 
} 

@SuppressWarnings("unchecked") 
public void loadEmployeeList(){ 
    employeeList = new ArrayList<FlowerStoreEmployee>(); 
    entityManager.isOpen(); 
    Query query = entityManager.createQuery("SELECT e FROM FlowerStoreVehicle e"); 

    employeeList.addAll((List<FlowerStoreEmployee>)query.getResultList()); 
} 

//Getters and Setters 

public List<FlowerStoreEmployee> getEmployeeList() { 
    return employeeList; 
} 

public void setEmployeeList(List<FlowerStoreEmployee> employeeList) { 
    this.employeeList = employeeList; 
} 

//used by the add button on the addEmp.xhtml page 
public String addEmployee(){ 
    int id = Integer.parseInt(empID); 
    int soc = Integer.parseInt(ssn); 
    int paid =Integer.parseInt(pay); 
    int vehID = Integer.parseInt(vehicle); 
    int houseID = Integer.parseInt(house); 
    Integer zCode = Integer.parseInt(zip); 
    FlowerStoreEmployee n = new FlowerStoreEmployee(); 
    n.setNameFirst(first); 
    n.setNameLast(last); 
    n.setPay(pay); 
    n.setPhone(phone); 
    n.setSsn(ssn); 
    employeeList.add(n); 
    entityManager.persist(n); 
    return "/employee.xhtml"; 
} 

//used by the remove button on the remEmp.xhtml page 
public String remEmployee(){ 
     int searchID = Integer.parseInt(empID); 
     int emp = 0; 
     int i = 0; 
     FlowerStoreEmployee e; 
     while (emp!= searchID && i<employeeList.size()){ 
      e = employeeList.get(i); 
      emp = e.getEmployeeId(); 
     } 
     employeeList.remove(i); 
     return "/employee.xhtml"; 
} 

//clears the variables used to temporarily store the information entered on the forms 
public void clearTemps(){ 
    this.empID = null; 
    this.first = null; 
    this.last = null; 
    this.ssn = null; 
    this.phone=null; 
    this.pay = null; 
    this.vehicle = null; 
    this.house = null; 
    this.street=null; 
    this.city=null; 
    this.state=null; 
    this.zip=null; 
} 


public String createEmp() { 
    clearTemps(); 
    System.out.println("words"); 
    return "FlowerStore/addEmp.xhtml"; 
} 


//Setters and getters 

public String getEmpID() { 
    return empID; 
} 

public void setEmpID(String empID) { 
    this.empID = empID; 
} 

public String getFirst() { 
    return first; 
} 

public void setFirst(String first) { 
    this.first = first; 
} 

public String getLast() { 
    return last; 
} 

public void setLast(String last) { 
    this.last = last; 
} 

public String getSsn() { 
    return ssn; 
} 

public void setSsn(String ssn) { 
    this.ssn = ssn; 
} 

public String getPhone() { 
    return phone; 
} 

public void setPhone(String phone) { 
    this.phone = phone; 
} 

public String getPay() { 
    return pay; 
} 

public void setPay(String pay) { 
    this.pay = pay; 
} 

public String getVehicle() { 
    return vehicle; 
} 

public void setVehicle(String vehicle) { 
    this.vehicle = vehicle; 
} 

public String getHouse() { 
    return house; 
} 

public void setHouse(String house) { 
    this.house = house; 
} 

public String getStreet() { 
    return street; 
} 

public void setStreet(String street) { 
    this.street = street; 
} 

public String getCity() { 
    return city; 
} 

public void setCity(String city) { 
    this.city = city; 
} 

public String getState() { 
    return state; 
} 

public void setState(String state) { 
    this.state = state; 
} 

public String getZip() { 
    return zip; 
} 

public void setZip(String zip) { 
    this.zip = zip; 
} 

/* Seam/Hibernate methods */ 
    @Remove 
    public void remove() { 
    } 

    @Destroy 
    public void destroy() { 
    } 


} 

如果需要任何其他信息,請讓我知道。

回答

1

注入不能在構造之前發生,你正試圖將一個組件注入到尚未創建的東西中。

嘗試改爲使loadEmployeeList成爲init方法並使用@PostConstruct對其進行註釋。 https://community.jboss.org/thread/141133