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.
您的关系应使用@ManyToOne
声明(例如,在“ projects
集合中的Document
”中),然后在JPA查询中使用inner join
@ManyToOne
,例如,为给定项目选择所有文档:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.