Issue
I have two entities user and movie. I want to display movies for the users. Users are going to see the movies they added. I connected these two enetities with manyToMany and i am trying to get the data but i am having this error.
2021-05-27 01:01:16.109 ERROR 4064 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: could not resolve property: user of: com.moviePage.movieP.Entities.Movie [from com.moviePage.movieP.Entities.Movie m join m.user u where u.id = 1]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: user of: com.moviePage.movieP.Entities.Movie [from com.moviePage.movieP.Entities.Movie m join m.user u where u.id = 1]] with root cause
org.hibernate.QueryException: could not resolve property: user of: com.moviePage.movieP.Entities.Movie
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:77) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:71) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:2038) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:412) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:520) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:695) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:269) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:209) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:114) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:109) ~[hibernate-core-5.4.30.Final.jar:5.4.30.Final]
...
User
@Entity
@Table(name="user")
public class User {
@Id
@Column(name="user_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@NotNull(message="Enter first name")
@Column(name="name")
private String name;
@ManyToMany
@JoinTable(
name="user_movie",
joinColumns =@JoinColumn(name="user_id"),
inverseJoinColumns=@JoinColumn(name="movie_id"))
private Set<Movie> movies;
Movie
@Entity
@Table(name="movie")
public class Movie {
@Id
@Column(name="movie_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="title")
private String title;
@Lob
@Column(name="summary")
private String summary;
@ManyToMany(mappedBy = "movies")
private Set<User> users;
getMovies => i put this in data access layer and call it from the controller and from the controller i am going to send the movies to the html page
public List<Movie> getMovies(int id) {
Session session = entityManager.unwrap(Session.class);
String hql = "from Movie m join m.user u where u.id = "+ String.valueOf(id);
List<Movie> movies = session.createQuery(hql).list();
System.out.println(movies);//tried to look what it will return
return movies;
}
//List<Movie> movies = session.createQuery("from Movie m join m.user u where u.user_id=:user_id").setParameter("id", id).getResultList();
i have tried these two and both produce the same error. I couldn't understand the error please help:)
Solution
You probably want:
String hql = "from Movie m join m.users u where u.id = "+ String.valueOf(id);
Answered By - Alberto Sinigaglia