newbie newbie - 29 days ago 18
AngularJS Question

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

Ok guys, i got something like this:

A student with ID, Name and Last name...
Now i made a new java class called ACCOUNTS 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 make the 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:

@GET
@Path("/getAll")
@Produces(MediaType.APPLICATION_JSON)
public List<Student> getAll() {
return em.createQuery("from Student s").getResultList();
}


and the controler for that looks like this:

$http.get('http://localhost:8080/CreditCardWEB/rest/cc/getAll').success(
function(data) {
$scope.result = data;
});


And it all worked...in 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?

Answer

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();