My related tables are below. Person Table
Location Table
Person_Location Table
I want to get person and location values like this..
id | name | surname | title | point
1 john | adas | my home | 44,45
1 | John | adas | brother's home | 55,33
How can I get the users and their locations in hibernate?
try this out :
you can get an array of object and you can manipulat them as you want
String stringQuery="select p.id,p.name,p.surname,l.title,l.point from person p, location l,person_location pl where "
+ "p.id=pl.person_id and l.id=pl.location_id";
Query query=entityManager.createNativeQuery(stringQuery);
List<Object[]> result=query.getResultList();
later you can get the person Id by result.get(i)[0]
or you can create a custom class which will not be a managed entity:
public class customPerson{
id | name | surname | title | point
private int id;
private String name;
private String surname;
private String title;
private String doube point;
//getters&setters
//constructors (required) one default ant one with all your attributes
public CustomPerson(){}
public customPerson(int id,...){
...
}
}
later in your Dao you can get the result you want through the custom object:
String stringQuery="select p.id,p.name,p.surname,l.title,l.point from person p, location l,person_location pl where "
+ "p.id=pl.person_id and l.id=pl.location_id";
Query query=entityManager.createNativeQuery(stringQuery,CustomPerson.class);
List<CustomPerson> result=query.getResultList();