我得到异常javax.servlet.ServletException javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)

时间:2015-09-10 22:42:56

标签: java xml spring hibernate servlets

所以我想从我的数据库中获取数据,但我总是得到这个异常,说我的getAll()方法返回null。我不知道该怎么办,有人可以帮帮我吗?

由于

WarehouseHasProduct.java

public class WarehouseHasProduct implements java.io.Serializable {

private Integer id;
private Product product = new Product();
private Warehouse warehouse = new Warehouse();
private int stock;

public WarehouseHasProduct() {
}

public WarehouseHasProduct(Product product, Warehouse warehouse, int stock) {
    this.product = product;
    this.warehouse = warehouse;
    this.stock = stock;
}

public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

public Product getProduct() {
    return this.product;
}

public void setProduct(Product product) {
    this.product = product;
}

public Warehouse getWarehouse() {
    return this.warehouse;
}

public void setWarehouse(Warehouse warehouse) {
    this.warehouse = warehouse;
}

public int getStock() {
    return this.stock;
}

public void setStock(int stock) {
    this.stock = stock;
}

}

Warehouse.java

public class Warehouse implements java.io.Serializable {

private Integer id;
private String description;
private Set<WarehouseHasProduct> warehouseHasProducts = new HashSet<WarehouseHasProduct>(
        0);

public Warehouse() {
}

public Warehouse(String description) {
    this.description = description;
}

public Warehouse(String description,
        Set<WarehouseHasProduct> warehouseHasProducts) {
    this.description = description;
    this.warehouseHasProducts = warehouseHasProducts;
}

public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public Set<WarehouseHasProduct> getWarehouseHasProducts() {
    return this.warehouseHasProducts;
}

public void setWarehouseHasProducts(
        Set<WarehouseHasProduct> warehouseHasProducts) {
    this.warehouseHasProducts = warehouseHasProducts;
}

}

Product.java

public class Product implements java.io.Serializable {

private Integer id;
private ProductType productType;
private String productName;
private String description;
private double price;
private Set<WarehouseHasProduct> warehouseHasProducts = new HashSet<WarehouseHasProduct>(
        0);
private Set<Order> orders = new HashSet<Order>(0);

public Product() {
}

public Product(ProductType productType, String productName,
        String description, double price) {
    this.productType = productType;
    this.productName = productName;
    this.description = description;
    this.price = price;
}

public Product(ProductType productType, String productName,
        String description, double price,
        Set<WarehouseHasProduct> warehouseHasProducts, Set<Order> orders) {
    this.productType = productType;
    this.productName = productName;
    this.description = description;
    this.price = price;
    this.warehouseHasProducts = warehouseHasProducts;
    this.orders = orders;
}

public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

public ProductType getProductType() {
    return this.productType;
}

public void setProductType(ProductType productType) {
    this.productType = productType;
}

public String getProductName() {
    return this.productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public double getPrice() {
    return this.price;
}

public void setPrice(double price) {
    this.price = price;
}

public Set<WarehouseHasProduct> getWarehouseHasProducts() {
    return this.warehouseHasProducts;
}

public void setWarehouseHasProducts(
        Set<WarehouseHasProduct> warehouseHasProducts) {
    this.warehouseHasProducts = warehouseHasProducts;
}

public Set<Order> getOrders() {
    return this.orders;
}

public void setOrders(Set<Order> orders) {
    this.orders = orders;
}

}

WarehouseHasProduct.hbm.xml

<hibernate-mapping>
<class name="com.app.hibernate.data.WarehouseHasProduct" table="warehouse_has_product" catalog="test">
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="identity" />
    </id>
    <many-to-one name="product" class="com.app.hibernate.data.Product" fetch="select">
        <column name="product_id" not-null="true" />
    </many-to-one>
    <many-to-one name="warehouse" class="com.app.hibernate.data.Warehouse" fetch="select">
        <column name="warehouse_id" not-null="true" />
    </many-to-one>
    <property name="stock" type="int">
        <column name="stock" not-null="true" />
    </property>
</class>
</hibernate-mapping>

Warehouse.hbm.xml

   <hibernate-mapping>
<class name="com.app.hibernate.data.Warehouse" table="warehouse" catalog="test">
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="identity" />
    </id>
    <property name="description" type="string">
        <column name="description" length="100" not-null="true" />
    </property>
    <set name="warehouseHasProducts" table="warehouse_has_product" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="warehouse_id" not-null="true" />
        </key>
        <one-to-many class="com.app.hibernate.data.WarehouseHasProduct" />
    </set>
</class>
</hibernate-mapping>

Product.hbm.xml

<hibernate-mapping>
<class name="com.app.hibernate.data.Product" table="product" catalog="test">
    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="identity" />
    </id>
    <many-to-one name="productType" class="com.app.hibernate.data.ProductType" fetch="select">
        <column name="type_id" not-null="true" />
    </many-to-one>
    <property name="productName" type="string">
        <column name="productName" length="100" not-null="true" />
    </property>
    <property name="description" type="string">
        <column name="description" length="100" not-null="true" />
    </property>
    <property name="price" type="double">
        <column name="price" precision="22" scale="0" not-null="true" />
    </property>
    <set name="warehouseHasProducts" table="warehouse_has_product" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="product_id" not-null="true" />
        </key>
        <one-to-many class="com.app.hibernate.data.WarehouseHasProduct" />
    </set>
    <set name="orders" table="order_has_product" inverse="false" lazy="true" fetch="select">
        <key>
            <column name="product_id" not-null="true" />
        </key>
        <many-to-many entity-name="com.app.hibernate.data.Order">
            <column name="order_id" not-null="true" />
        </many-to-many>
    </set>
</class>
</hibernate-mapping>

WarehouseHasProductService.java

@Component
public class WarehouseHasProductService {
@Autowired
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

@Transactional
public void register(WarehouseHasProduct whp){
    // Acquire session
    Session session = sessionFactory.getCurrentSession();
    // Save employee, saving behavior get done in a transactional manner
    session.save(whp);      
}

@SuppressWarnings("unchecked")
@Transactional
public List<WarehouseHasProduct> getAll(){
    // Acquire session
    Session session = sessionFactory.getCurrentSession();

    List list = session.createQuery("from com.app.hibernate.data.WarehouseHasProduct").list();

    return list;
}
}

RegisterWarehouseHasProduct.java

@ManagedBean
@SessionScoped
public class RegisterWarehouseHasProduct{

@ManagedProperty("#{whpService}")
private WarehouseHasProductService whpService;

private WarehouseHasProduct whp = new WarehouseHasProduct();

public WarehouseHasProductService getWhpService() {
    return whpService;
}

public void setWhpService(WarehouseHasProductService whpService) {
    this.whpService = whpService;
}

public WarehouseHasProduct getWhp() {
    return whp;
}

public void setWhp(WarehouseHasProduct whp) {
    this.whp = whp;
}

public String register() {
    // Calling Business Service
    whpService.register(whp);

    // Add message
    FacesContext.getCurrentInstance().addMessage(null, 
            new FacesMessage("The Product "+whp.getId()+" Is Registered Successfully"));
    return "";
}

public List<WarehouseHasProduct> getAll(){
    return whpService.getAll();
}

}

Home.xhtml

 <p:dataTable var="p" value="#{registerProduct.getAllProducts()}">

    <f:facet name="header">
        Products List
    </f:facet>
    <p:column headerText="Code">
        <h:outputText value="#{p.id}" />
    </p:column>

    <p:column headerText="Name">
        <h:outputText value="#{p.productName}" />
    </p:column>

    <p:column headerText="Description">
        <h:outputText value="#{p.description}" />
    </p:column>

    <p:column headerText="Type">
        <h:outputText value="#{p.productType.description}" />
    </p:column>

    <p:column headerText="Price">
        <h:outputText value="#{p.price}" />
    </p:column>
</p:dataTable>

我真的很感激答案。谢谢!

0 个答案:

没有答案