在人类
@OneToMany(mappedBy="person") public Set<personLocations> getPersonLocation { return personLocations; }
在Location类中
@OneToMany(mappedBy="location") public Set<personLocations> getPersonLocation { return personLocations; }
在人的地方
@ManyToOne @JoinColumn(name="person_ID") public Person getPerson() { return person; } @ManyToOne @JoinColumn(name="location_ID") public Location getlocation() { return location; }
试试这个:
你可以获得一个对象数组,你可以根据需要操作它们
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();
以后你可以得到这个人的身份 result.get(i)[0]
result.get(i)[0]
或者您可以创建一个不是托管实体的自定义类:
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,...){ ... } }
稍后在您的Dao中,您可以通过自定义对象获得所需的结果:
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();