【问题标题】:Getting error in Hibernate query. Error: org.hibernate.hql.ast.QuerySyntaxException在 Hibernate 查询中出现错误。错误:org.hibernate.hql.ast.QuerySyntaxException
【发布时间】:2014-07-23 17:29:27
【问题描述】:

我正在尝试在 SpringMvc 模型类中实现多对一关系。但是我找不到任何可以理解其工作原理的示例。

return (Student_Course) session.getCurrentSession().createQuery("from Student_Course as sc where c.user.id=?").setInteger(0, user_id).uniqueResult()

在上面的代码中,我试图通过休眠查询从数据库中检索 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 查询,以便获取此表。请帮帮我。

课程班:

@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 班级:

@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();
    }

}

【问题讨论】:

    标签: java mysql hibernate hql


    【解决方案1】:

    你的别名是sc,你的列名是user_id,所以应该是

    "from Student_Course as sc where sc.user_id=?"
    

    改为。

    【讨论】:

    • 试着像下面那样做
    • 它正在工作..我在那里犯了一个错误...非常感谢你的朋友。
    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多