JPA + EJB + JSF: how can design complicated query
- by Harry Pham
I am using netbean 6.8 btw.
Let say that I have 4 different tables: Company, Facility, Project, and Document. So the relationship is this. A company can have multiple facilities. A facility can have multiple projects, and a project can have multiple documents.
Company:
+companyNum: PK
+facilityNum: FK
Facility:
+facilityNum: PK
+projectNum: FK
Project:
+projectNum: PK
+drawingNum: FK
So when I create Entity Class From Database in netbean 6.8, I have 4 entity classes that named after the above 4 tables. So if I want to see all the Document in the database, then it is easy. In my SessionBean, I would do this:
@PersistenceContext
private EntityManager em;
List<Document> documents = em.createNamedQuery("Document.findAll").getResultList();
However, that is not all what I need. Let say that I want to know all the Document from a particular Company, or all the Document from a particular Project from a particular Facility from a particular Company. I am very new to JPA + EJB + JSF as a whole. Please help me out.