JPA many to one: get elements

I have a problem, that I would like to explain with the following example: JPA class Person: @ManyToMany(mappedBy = "persons") private List cars= new ArrayList<>(); public void setCars(List cars) { this.cars= cars; } public List getCars() { return cars; } and where the problem shows itself: Servlet class: Person person = genericDao.findOne(1); request.setAttribute("person", person.getName()); List cars= new ArrayList(); cars= person.getCars(); request.setAttribute("cars", cars); The problem is that when trying to get the cars. They aren't automatically retrieved from the database and saved in the array in the Person class. How can I setup a JPA class with a Many to Many relationship, from which I can easily retrieve objects with a relationship. Car.java IMPORTS LEFT OUT! @Entity @Table(name = "DSD_Cars") public class Car extends ModifiableEntity { private String name; private String description; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @ManyToMany @JoinTable( name="DSD_Persons_Cars", joinColumns = {@JoinColumn(name="IDCar", referencedColumnName = "ID")}, inverseJoinColumns = {@JoinColumn(name = "IDPerson", referencedColumnName = "ID")} ) private List persons= new ArrayList<>(); public void setPersons(List persons) { this.persons= persons; } public List getPersons() { return persons; }}
Can you show the Car Entity?

以上就是JPA many to one: get elements的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏