我正在尝试在 SpringMvc 模型类中实现多对一关系。但我找不到任何可以理解其工作原理的示例。
return (Student_Course) session.getCurrentSession().createQuery("from Student_Course as sc where c.user.id=?").setInteger(0, user_id).uniqueResult()
在上面的代码中,我试图通过 hibernate 查询从数据库中检索 Student_Course 表,但它给了我这个错误。
错误
org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'c.user.id' [from com.sanjay31321.sys.model.Student_Course as sc where c.user.id=?]
有没有其他方法可以编写这个 hql 查询,以便我可以获得这个表。请帮助我。
类(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
}
用户类别:
@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 类
@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();
}
}
最佳答案
您的别名是 sc
,您的列名称是 user_id
,所以应该是
"from Student_Course as sc where sc.user_id=?"
相反。
关于java - Hibernate 查询中出现错误。错误 : org. hibernate.hql.ast.QuerySyntaxException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24917093/