使用MySQL Query通过CustomerId获取信息

时间:2013-11-22 12:55:40

标签: mysql java-ee-6

我有3个实体客户,数据和信息。实体之间的关系是oneToMany,如下所示:

public class Customer implements Serializable {
 ...
 @OneToMany
 private List<Data> datas;
 ...

public class Data implements Serializable {
...
@OneToMany(mappedBy = "dataBase")
    private List<Information> informations;
    @ManyToOne
    private Customer customer;
..

public class Information implements Serializable {
...
@ManyToOne
    private Data dataBase;
...

现在我想要登录的每个客户只能看到自己的信息。 我在想使用JPQL命名查询。 所以我在manged Bean of information中编写这个方法

public List<Information> getInformations() {  
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest)      context.getExternalContext().getRequest();
        HttpSession session = request.getSession(false);
        String idCustomer = (String) session.getAttribute("idCustomer");
        Customer cust = customerBusinessLocal.findById(idCustomer);
        List<Data> datas=dataBusinessLocal.findByCustomer(cust);
        return InformationBusinessLocal.informations(datas);      
    }

但我得到了javax.ejb.EJBTransactionRolledbackException

1 个答案:

答案 0 :(得分:-1)

  public List<Information`enter code here`> getInformations() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        HttpSession session = request.getSession(false);
        String idCustomer = (String) session.getAttribute("idCustomer");
        System.out.println(idCustomer + " this is it");
        Customer cust = customerBusinessLocal.findById(idCustomer);
        List<Data> datas = dataBusinessLocal.findByCustomer(cust);
        for(Data d: datas)
         {           
             for(Information i: d.getInformations()){          
                 informations.add(i);
         } 
             } 
        return informations;
    }