2013-11-22 56 views
0

我有3个实体客户,数据和信息。实体之间的关系是oneToMany是这样的:使用MySQL Query获取客户信息

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; 
... 

现在我希望每个登录的客户只能看到自己的信息。 我想用abql使用JPQL命名查询。 所以我写的信息

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);  
    } 

的manged Bean的这个方法,但我得到javax.ejb.EJBTransactionRolledbackException

+0

如何使用教程开始? – Strawberry

+0

你知道任何有用的吗? –

+0

我相信任何使用适当关键字找到的教程都会很有用。 – Strawberry

回答

-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; 
    }