view raw
newbie newbie - 6 months ago 22
AngularJS Question

How to preform the JOIN FETCH query to get the correct result?

Ok guys, I got something like this:

A student with ID, Name and Last name...

Now I created a new java class called

with Id, Name and Value

There is also this:

@OneToMany(cascade = { CascadeType.ALL})
private Set<BankAccout> accounts;

public Student() {
accounts= new HashSet<BankAccout>();

public Set<BankAccout> getAccounts() {
return accounts;

public void setAccounts(Set<BankAccout> accounts) {
this.accounts= accounts;

Now, I have my site, on the site there is a table with the data of all students... they are all unique

Then I had to write a new class, and I have to add a listBox to my site, that shows the selected students accounts...

My old query for calling the student looked like this:

public List<Student> getAll() {
return em.createQuery("from Student s").getResultList();

and the controller for that looks like this:

function(data) {
$scope.result = data;

And it all the table there were no problems....but i had to change the query to be able to bind the data from the accounts to the table....and now the query looks like this...

return em.createQuery("from Student s inner join fetch s.accounts").getResultList();

and now, the table SHOULD show me the same data as before...that is, the all students that are all unique....but it shows me only those students that have at least one account....and not only that, but the same student is shown not once, but more times (the number he has accounts) in the table...

Any suggestions?


An INNER JOIN filters off records from either table which do not match. Hence, your current join is removing students who do not have an account. The way around this is to use a LEFT JOIN, which will retain all students even if they do not have an account:

return em.createQuery("from Student s left join fetch s.accounts").getResultList();