Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to load all objects in JPA 2.0 using Criteria
How to load all objects in JPA 2.0 using Criteria [message #521226] Tue, 16 March 2010 15:34 Go to next message
Nobili  is currently offline Nobili
Messages: 6
Registered: March 2010
Junior Member
I have the following code using JPA 2.0 with Criteria:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPU");
EntityManager em = emf.createEntityManager();

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<System> c = cb.createQuery(System.class);
Root<System> r = c.from(System.class);
ListJoin<System, Report> reports = r.joinMap("reports");
c.select(r);

List<Predicate> list = new ArrayList<Predicate>();

Path<String> nameField1 = r.get("name");
ParameterExpression<String> param1 = cb.parameter(String.class,
"param1");
lista.add(cb.equal(cb.upper(nameField1), param1));

Path<String> nameField2 = reports.get("name");
ParameterExpression<String> param2 = cb.parameter(String.class,
"param2");
lista.add(cb.equal(nameField2, param2));

c.where(cb.and(lista.toArray(new Predicate[0])));

TypedQuery<System> t = em.createQuery(c);
t.setParameter("param1", "MySystem");
t.setParameter("param2", "Developer");


The code works well. However, I'm having a problem to retrieve all reports from system. My goal is to get always all entitie objects according to my needs, without having to load them in "lazy" mode. This would be independent of join quantities.

I've tried several alternatives like using "fetch of Root interface". However, when the query is showed on log, I note that the table "Report" is showed twice in the "from" clause. I can't find a solution to solve it.

For that reason, I'd like to know if somebody should help me with this question. Even through an EclipseLink native solution. I know it isn't the better alternative but would immediately solve my problem.
Re: How to load all objects in JPA 2.0 using Criteria [message #521233 is a reply to message #521226] Tue, 16 March 2010 16:29 Go to previous message
Tom Ware is currently offline Tom Ware
Messages: 17
Registered: July 2009
Junior Member
Is this the criteria-based question for the query you list here?

http://www.eclipse.org/forums/index.php?t=msg&th=164235& amp;start=0&

If what SQL are you hoping will be produced?

Can you write SQL that will allow you to fetch join reports and not have two references to the REPORT table in the following case?

- System has two reports
- Report1 with name="Development"
- Report2 with name="SomethingElse"

What SQL would you expect to retreive System and both reports?
Previous Topic:Can eclipselink log entity changes?
Next Topic:External file to specify DB information
Goto Forum:
  


Current Time: Wed Jul 23 22:25:30 EDT 2014

Powered by FUDForum. Page generated in 0.01609 seconds