JSF Datatable无法获取OnetoMany关系对象的值?

时间:2011-05-02 12:59:51

标签: jsf datatable one-to-many

我是一名JSF新手,在使用数据表显示数据时遇到问题。这是我的情景:

我想显示分配给客户的合约

JSF页面的数据表:customerdetail.jsp

 <h:dataTable id="dt_contract_list" value="#{customerBean.customer.contracts}" var="item">
    <h:column>
        <f:facet name="header">
            <h:outputText value="Contract Identifier"/>
        </f:facet> 
        <h:outputText style="" value="#{item.contractIdentifier}"></h:outputText>
    </h:column>
 </h:dataTable>

实体Bean:Customer.java

@Entity
@NamedQueries({
    @NamedQuery(name="getCustomerByName", query="SELECT customer FROM Customer customer WHERE customer.name = :name"),
    @NamedQuery(name="getAllCustomer", query="SELECT customer FROM Customer customer")
})
public class Customer {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;

    @OneToMany(targetEntity = Contract.class, 
               mappedBy = "customer", 
               cascade = CascadeType.ALL,
               fetch=FetchType.EAGER)
    private List<Contract> contracts = new ArrayList<Contract>();

    public Customer() {
        super();
    }

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

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

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Contract> getContracts() {
        return this.contracts;
    }

    @Override
    public boolean equals(Object object) {
        if (this.getClass().isInstance(object)) {
            return this.getName().equals(((Customer) object).getName());
        } else {
            return false;
        }
    }   
}

BackingBean:CustomerBean.java

public class CustomerBean implements Serializable {

    @EJB
    private CustomerControllerLocal customerController;

    private Customer customer;

    public CustomerBean()  {
        customer = new Customer();
    }

    public String createCustomer()  {
        // create customer
    }

    public String updateCustomer()  {
        // update customer
    }

    public String deleteCustomer()  {
        // delete custoer
    }

    public String getCustomerByName()  {
        try {
            customerController.getCustomerByName(customer.getName());
        } catch (NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "customer_got";
    }

    public String assignContractsToCustomer()  {
        // assign contract to customer
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
}

没有错误,整个业务逻辑层,数据层都经过测试并且工作正常,但结果为空(并且它不应该!)并且没有任何异常的迹象。任何人都可以帮我弄明白这有什么问题吗? Ť___Ť

1 个答案:

答案 0 :(得分:0)

您的facelet代码看起来不错。

在您的实体类中,有一件事看起来很可疑。这是这一行:

private List<Contract> contracts = new ArrayList<Contract>();

在我自己的项目的实体classe(由netbeans生成的类)中,关系列表永远不会被初始化。我不是JPA的专家,但你可以在没有初始化部分的情况下尝试它。