0

I have the following 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;
    }

}

The CustomerOrder class is a JPA class:

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 

}

The JPA class constructor CustomerOrder(Email email) is called by Quartz when a new email message is received. I tested this and this works. I have some customerorder records in my database.

Finally my JSF page:

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

I am expecting this to output the customerId fields from thetable with customerorders in the database. However, it just returns an empty table.

I would at least expect that in my server terminal the Sysout messages from the bean are shown, but even these are not shown.

Question: What is wrong with my code, why are my database entry's not shown? How can I debug this problem?

Martijn Burger
  • 7,315
  • 8
  • 54
  • 94
  • i cannot see any `f:view` tags on your jsf page. can you debug OrderBean.java, does the application hit the constructor? – tt_emrah Jul 31 '14 at 11:24
  • No, it does not hit the constructor. Why would I need an f:view tag? According to [this](http://stackoverflow.com/questions/8883476/when-to-use-fview-and-fsubview) post it is required for JSP pages, not for JSF. – Martijn Burger Jul 31 '14 at 11:33
  • well, it is optional indeed, sorry about jumping to the conclusion. anyway, since it doesn't even hit the managed bean constructor, i would check if the FacesServlet url pattern in web.xml actually matches your page url. – tt_emrah Jul 31 '14 at 12:00
  • Sorry, but that's not it. My URL pattern is *.jsf. When I request test.jsf (pagename is test.xhtml) I get valid html code in return. – Martijn Burger Jul 31 '14 at 12:20
  • which jsf implementation are you using? it works for me when i add `@ManagedBean` to OrderBean, and change `xmlns:h="http://xmlns.jcp.org/jsf/html"` to `xmlns:h="http://java.sun.com/jsf/html"` (i'm using mojarra 2.1.7). – tt_emrah Jul 31 '14 at 13:39
  • Tried changing the namespace to the old (PreJSF 2.2 namespace) and changed my Mojarra version (i was running 2.2.7) back to 2.1.7, no changes in the outcome. :( – Martijn Burger Jul 31 '14 at 14:21
  • I also changed the bean to the old style JSF annotation instead of CDI. No luck. – Martijn Burger Jul 31 '14 at 14:29
  • the only time i get an empty table is when the `@ManagedBean` annotation is missing. :/ – tt_emrah Jul 31 '14 at 14:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58433/discussion-between-martijn-burger-and-tt-emrah). – Martijn Burger Jul 31 '14 at 14:31
  • Thanks for all the effort. I cannot seem to figure out what the problem is. :( – Martijn Burger Aug 01 '14 at 09:53

1 Answers1

1

using java 8, eclipse luna, tomcat 8 and mojarra 2.2.7; after importing @ManagedBean from javax.faces.bean.ManagedBean instead of javax.annotations.ManagedBean, it works just fine.

tt_emrah
  • 1,043
  • 1
  • 8
  • 19