2

It's a dum down version of a real problem

I have Three Tables STUDENT ADDRESS CLASS

STUDENT ManyToOne ADDRESS STUDENT ManyToMany CLASS

I need to bring all the students who have same address and go to same class.

Sql would simply be (just writing this on the fly and have not tested it)

Select * from STUDENT s join ADDRESS a on s.addressId = a.addressId join CLASS c on c.classId = s.classId

Now I am new to Criteria and while have been able use it reasonbly so far , I am totally stuck as how should I do this join have looked every where documentation by Oracle , Jboss etc but no indication on how this should be done and all the stabs I have taken in the dark have been unsuccessful. Even on SO there are many questions in similar tone but when you read them it is something else thats being ask.

Java Ka Baby
  • 4,880
  • 11
  • 39
  • 51

2 Answers2

0

Assuming that your Student class defines a collection that is managed by JPA, you shouldn't need to define any joins in your JPQL query. Just retrieve the Student object and OpenJPA will retrieve the addresses and classes automatically.

Templar
  • 5,067
  • 7
  • 34
  • 39
0

You can try this:

Root<STUDENT> from = criteriaQuery.from(STUDENT.class);
Join<STUDENT, ADDRESS> joinAddress = from.join(STUDENT_.address);
Join<STUDENT, CLASS> joinClass = from.join(STUDENT_.class);

Before this you should generate the meta classes from your entities.

javac -classpath "PATHTOLIBRARIES\openjpa-all-2.2.2.jar" -Aopenjpa.metamodel=true STUDENT.java ADDRESS.java CLASS.java

PS: if you don't need to do anything extra with the result of 'from.join' you can just ignore the resurned value.

smiron
  • 408
  • 3
  • 13