I am trying to get data from multiple tables in Spring Data JPA.
User.java
@Entity
@Table(name = "user")
public class User{
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String username;
private String password;
//Getters and setters generated
@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
private List<Classes> classes;
Classes.java
@Entity
public class Classes {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String name;
@Lob
private String description;
@ManyToMany(mappedBy = "classes", fetch = FetchType.EAGER, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
private List<User> user;
These entities generate table named as 'user_classes' and fields as following classes_id and user_id
I am trying to display name of classes that belong to that particular user. As I have my models designed as shown above I am trying to do it like this
<tr th:each="classes : ${user.classes}">
<td th:text="${classes.name}"></td>
</tr>
My table in database looks like this
So class and user do exist.
But I am facing this error
EL1007E: Property or field 'classes' cannot be found on null
I am not sure what I am doing wrong?
Mistake was in spelling. I have passed User object Thymeleaf view as
<tbody th:each="users : ${users}">
And I was trying to reach it as
<tr th:each="classes : ${user.classes}">
<td th:text="${classes.name}"></td>
</tr>
Where part ${user.classes}
didn't work. When I wrote user object as users
or like this ${users.classes}
everything worked!