Good day. There are three tables: "cars", "details" and "detail_car". Each table has an id, the "detail_car" still has car_id and detail_id columns. Those. cars and details are combined via the "detail_car" table. Here is the Car class:

@Entity @Table(name="CARS") public class Car { @Id @Column(name="id") private int id; @ManyToMany(cascade = CascadeType.ALL) @JoinTable( name = "detail_car", joinColumns = @JoinColumn(name = "car_id", referencedColumnName="id"), inverseJoinColumns = @JoinColumn(name = "detail_id", referencedColumnName="id")) private List<Detail> details; } 

In this case, the server console displays

  SELECT this_.id AS id1_0_2_, this_.about AS about2_0_2_, this_.img_url AS img_url3_0_2_, this_.mark_id AS mark_id6_0_2_, this_.model_id AS model_id7_0_2_, this_.NICK AS NICK4_0_2_, this_.year AS year5_0_2_, mark2_.id AS id1_4_0_, mark2_.MARK AS MARK2_4_0_, model3_.id AS id1_5_1_, model3_.MODEL AS MODEL2_5_1_ FROM CARS this_ LEFT OUTER JOIN Marks mark2_ ON this_.mark_id=mark2_.id LEFT OUTER JOIN Models model3_ ON this_.model_id=model3_.id WHERE this_.id=? 

The error is: Unable to evaluate the expression Method threw 'org.hibernate.LazyInitializationException' exception.

How in the Car model to get the list of details belonging to this particular machine?

  • Well, you already received them. private List<Detail> details precisely, you declared them and linked them to the Hibernate table. If everything is correct, you will fill in the details variable with data. It remains only to add getters and setters - JVic
  • Where does the error fall? sample code? - JVic
  • With the debugger, I check the Car object, all the fields are there (in the class, both the heteros and the setters are on all fields of the table). I try to look into the values ​​of the details, and there Unable to evaluate the expression Method threw 'org.hibernate.LazyInitializationException' exception. - ks_on_v
  • Use the Hibernate.initialize(details) structure Hibernate.initialize(details) before using the parts list - JVic
  • In the controller I wrote the following: @RequestMapping (value = {"/ show- {id} -car"}, method = RequestMethod.GET) public String showCat (@PathVariable int id, ModelMap model) {Car car = carService.getCar (id ); List <Detail> details = car.getDetails (); Hibernate.initialize (details); model.addAttribute ("car", car); return "showCar"; } dropped HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: collection is not associated with any session - ks_on_v

0