tags:

views:

54

answers:

2

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.

+1  A: 

Your relationships should be declared using @ManyToOne (e.g. in Document on projects collection) and then use inner join in JPA queries, e.g. selecting all document for given project:

select d from Document d inner join d.projects p where p.id = ?
grigory
do you think you can provide a bit more sample codes?
Harry Pham
+1  A: 

In my opinion, the Chapter 27 - The Java Persistence Query Language from The Java EE Tutorials is a decent introduction and will help you to get started with JPQL. This is actually where you should start.

Pascal Thivent