数据库中的JSF bean未显示

时间:2014-07-31 10:32:20

标签: jsf jpa

我有以下bean:

import java.util.List;
import javax.faces.bean.RequestScoped;
import javax.annotations.ManagedBean;
import javax.persistence.EntityManager;
import listener.EMF;
import model.CustomerOrder;

@MangedBean
@RequestScoped
public class OrderBean {

    private List<CustomerOrder> orderList;

    /**
     * Creates a new instance of OrderBean
     */
    public OrderBean() {
        EntityManager em = EMF.createEntityManager();
        this.orderList = em.createNamedQuery("CustomerOrder.findAll").getResultList();
        System.out.println("=== Orderlist ===");
        for (CustomerOrder order : orderList) {
            System.out.println(order.getNumber());
        }
        em.close();
    }

    public List<CustomerOrder> getOrderList() {
        System.out.println("=== In getOrderList ===");
        return orderList;
    }

    public void setOrderList(List<CustomerOrder> orderList) {
        this.orderList = orderList;
    }

}

CustomerOrder类是JPA类:

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.*;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import listener.EMF;
import regex.AttachmentAnalyzer;

@Entity
@Table(name = "customerorder")
@NamedQueries({
    @NamedQuery(name = "CustomerOrder.findAll", query = "SELECT c FROM CustomerOrder c"),
    @NamedQuery(name = "CustomerOrder.findById", query = "SELECT c FROM CustomerOrder c WHERE c.id = :id"),
    @NamedQuery(name = "CustomerOrder.findByNumber", query = "SELECT c FROM CustomerOrder c WHERE c.number = :number"),
    @NamedQuery(name = "CustomerOrder.findByCalculationparameter", query = "SELECT c FROM CustomerOrder c WHERE c.calculationparameter = :calculationparameter"),
    @NamedQuery(name = "CustomerOrder.findByActive", query = "SELECT c FROM CustomerOrder c WHERE c.active = :active"),
    @NamedQuery(name = "CustomerOrder.findByLastupdate", query = "SELECT c FROM CustomerOrder c WHERE c.lastupdate = :lastupdate")})
public class CustomerOrder implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Size(max = 255)
    @Column(name = "number")
    private String number;
    @Column(name = "calculationparameter")
    private BigDecimal calculationparameter;
    @Column(name = "active")
    private Short active;
    @Basic(optional = false)
    @NotNull
    @Column(name = "lastupdate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastupdate;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "customerorderId")
    private List<Orderline> orderlineList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "customerorderId")
    private List<Attachment> attachmentList;
    @JoinColumn(name = "lastupdateby", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private User lastupdateby;
    @JoinColumn(name = "customer_id", referencedColumnName = "id")
    @ManyToOne
    private Customer customerId;
    @Transient
    private boolean validOrder;

    public CustomerOrder() {
    }

    public CustomerOrder(Email email) {
        this.attachmentList = email.getAttachments();
        EntityManager em = EMF.createEntityManager();
        List<Customer> customers = em.createNamedQuery("Customer.findAll").getResultList();
        User systemUser = (User) em.createNamedQuery("User.findByName").setParameter("name", "system").getSingleResult();
        em.close();
        for (Customer customer : customers) {
            String fromAddressFilter = customer.getEmailaddressfilter();
            String subjectFilter = customer.getEmailsubjectfilter();
            String subject = email.getSubject();
            String content = email.getContent();
            if (isMatch(email.getSubject(), subjectFilter)
                    && isMatch(email.getFromAddress(), fromAddressFilter)) {
                this.validOrder = true;
                this.active = 1;
                AttachmentAnalyzer analyzer = new AttachmentAnalyzer(customer, subject, content, attachmentList);
                this.number = analyzer.getNumber();
                this.calculationparameter = analyzer.getCalculationParameter();
                this.orderlineList = analyzer.getOrderlineList();
                this.customerId = customer;
                this.lastupdateby = systemUser;
                for (Attachment a : attachmentList) {
                    a.setCustomerorderId(this);
                }
                for (Orderline o : orderlineList) {
                    o.setCustomerorderId(this);
                    o.setLastupdateby(systemUser);
                }
            } else {
                this.validOrder = false;
            }
        }
    }

    private boolean isMatch(String string, String filter) {
        Pattern pattern;
        pattern = Pattern.compile(filter);
        Matcher matcher = pattern.matcher(string);
        boolean isMatch = matcher.find();
        return isMatch;
    }


// getters, setters and overrided hashCode, equal and toString methods omitted 

}

当收到新的电子邮件时,Quartz会调用JPA类构造函数CustomerOrder(电子邮件电子邮件)。我测试了这个,这很有效。我的数据库中有一些客户记录。

最后我的JSF页面:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Test page</title>
    </h:head>
    <h:body>
        <h:dataTable var="order" value="#{orderBean.orderList}" >
            <h:column>
                #{order.customerId}
            </h:column>
        </h:dataTable>
    </h:body>
</html>

我希望这能从数据库中的customrorders输出customer表中的customerId字段。但是,它只返回一个空表。

我至少会期望在我的服务器终端中显示来自bean的Sysout消息,但即使这些消息也未显示。

问题:我的代码有什么问题,为什么我的数据库条目没有显示?我该如何调试此问题?

1 个答案:

答案 0 :(得分:1)

使用java 8,eclipse luna,tomcat 8和mojarra 2.2.7; 从@ManagedBean而不是javax.faces.bean.ManagedBean导入javax.annotations.ManagedBean后,它就可以了。