Reputation: 425
I am trying to implement many-to-one relationship in SpringMvc model class. But i am not able to find any example from which i could able to understand how this work.
return (Student_Course) session.getCurrentSession().createQuery("from Student_Course as sc where c.user.id=?").setInteger(0, user_id).uniqueResult()
In above code i am trying to retreive Student_Course table from database through hibernate query but it is giving me this error.
Error
org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'c.user.id' [from com.sanjay31321.sys.model.Student_Course as sc where c.user.id=?]
Is there any other way i can write this hql query so i can get this table. Please help me.
Course Class :
@Entity @Table (name="Course")
public class Course {
@Id @Column @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="course_name")
private String name;
//Setter and Getter
}
Student_Course Class:
@Entity @Table (name="Student_course")
public class Student_Course {
@Id @Column @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@ManyToOne @JoinColumn(name="user_id")
private User user;
@ManyToOne @JoinColumn(name="course_id")
private Course course;
//Setter and Getter
}
User Class:
@Entity @Table (name="user")
public class User {
@Id @Column @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="email") @Email
private String email;
@Column(name="password")
private String password;
//Setter and Getter
}
StudentCourseDaoImpl Class
@Repository
public class StudentCourseDaoImpl implements StudentCourseDao{
@Autowired private SessionFactory session;
@Override
public Student_Course getStudentCourseByUserID(int user_id) {
return (Student_Course) session.getCurrentSession().createQuery("from Student_Course as sc where c.user.id=?").setInteger(0, user_id).uniqueResult();
}
}
Upvotes: 1
Views: 2357
Reputation: 10336
Your alias name is sc
, your column name is user_id
, so it should be
"from Student_Course as sc where sc.user_id=?"
instead.
Upvotes: 2