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
:
Facility
:
Project
:
So when I create Entity Class From Databas开发者_运维问答e
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.
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 = ?
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.
精彩评论