【问题标题】:errror in converting SQL query to HQL, Java Hibernate [closed]将 SQL 查询转换为 HQL、Java Hibernate 时出错 [关闭]
【发布时间】:2021-04-04 06:24:31
【问题描述】:

我有一个 Spring MVC + JDBC 应用程序。我在一所大学的数据库和 GroupDao 的数据库中有讲座和小组表格,并带有 LectureDao 课程。现在我正在尝试用 Hibernate 替换 JDBC。我有一个 SQL 查询

private static final String GET_ALL_GROUPS_FOR_LECTURE = "SELECT * FROM GROUPS " +
            "INNER JOIN LECTUREGROUPS ON GROUPS.ID = LECTUREGROUPS.GROUPID " +
            "WHERE LECTUREID = ?";

现在我已经像这样将它转换成 HQL

private static final String GET_ALL_GROUPS_FOR_LECTURE = "FROM Group " +
            "JOIN LectureGroup ON Group.id = LectureGroup.groupId " +
            "WHERE LectureGroup.lectureId = :lectureId";

我有使用这个查询的方法

public List<Group> getGroupsOnLecture(int lectureId) {
    try (Session session = sessionFactory.openSession()) {
        Transaction transaction = session.beginTransaction();
        Query<Group> query = session.createQuery(GET_ALL_GROUPS_FOR_LECTURE, Group.class);
        query.setParameter("lectureId", lectureId);
        List<Group> groupsOnLecture = query.list();
        transaction.commit();
        return groupsOnLecture;
    }
}

运行应用程序时出现异常。错误显示在这个带有 queryCreation 的原始文件中。所以HQL语法中的错误。我做错了什么?

这是 Lecture 和 Group 以及 LectureGroup 实体

@Entity
@Table(name = "groups")
public class Group {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;
    @Column(name = "name")
    private String name;
    @Column(name = "streamId")
    private int streamId;

    public Group(String name, int streamId) {
        this.name = name;
        this.streamId = streamId;
    }

    public Group() {
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public String getStringId() {
        return String.valueOf(id);
    }

    public int getStreamId() {
        return streamId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setStreamId(int streamId) {
        this.streamId = streamId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Group group = (Group) o;
        return id == group.id &&
                streamId == group.streamId &&
                name.equals(group.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, streamId);
    }

    @Override
    public String toString() {
        return "Group{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", streamId=" + streamId +
                '}';
    }

}
@Entity
@Table(name = "lectures")
public class Lecture {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;
    @Column(name = "subjectId")
    private int subjectId;
    @Column(name = "professorId")
    private int professorId;
    @Column(name = "date")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate date;
    @Column(name = "time")
    @DateTimeFormat(pattern = "HH:mm")
    private LocalTime time;
    @Column(name = "classroomId")
    private int classroomId;

    public Lecture(int subjectId, int professorId, LocalDate date, LocalTime time, int classroomId) {
        this.subjectId = subjectId;
        this.professorId = professorId;
        this.date = date;
        this.time = time;
        this.classroomId = classroomId;
    }

    public Lecture() {
    }

    public int getId() {
        return id;
    }

    public int getSubjectId() {
        return subjectId;
    }

    public int getProfessorId() {
        return professorId;
    }

    public LocalDate getDate() {
        return date;
    }

    public String getDateString() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
        return date.format(formatter);
    }

    public LocalDate setDateFromString(String dateString) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
        return LocalDate.parse(dateString, formatter);
    }

    public LocalTime getTime() {
        return time;
    }

    public int getClassroomId() {
        return classroomId;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setSubjectId(int subjectId) {
        this.subjectId = subjectId;
    }

    public void setProfessorId(int professorId) {
        this.professorId = professorId;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }

    public void setTime(LocalTime time) {
        this.time = time;
    }

    public void setClassroomId(int classroomId) {
        this.classroomId = classroomId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Lecture lecture = (Lecture) o;
        return id == lecture.id &&
                subjectId == lecture.subjectId &&
                professorId == lecture.professorId &&
                classroomId == lecture.classroomId &&
                date.equals(lecture.date) &&
                time.equals(lecture.time);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, subjectId, professorId, date, time, classroomId);
    }

    @Override
    public String toString() {
        return "Lecture{" +
                "id=" + id +
                ", subjectId=" + subjectId +
                ", professorId=" + professorId +
                ", date=" + date +
                ", time=" + time +
                ", classroomId=" + classroomId +
                '}';
    }

}
@Entity
@Table(name = "lecturegroups")
public class LectureGroup {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;
    @Column(name = "lectureId")
    private int lectureId;
    @Column(name = "groupId")
    private int groupId;

    public LectureGroup(int lectureId, int groupId) {
        this.lectureId = lectureId;
        this.groupId = groupId;
    }

    public LectureGroup() {
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setLectureId(int lectureId) {
        this.lectureId = lectureId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public int getId() {
        return id;
    }

    public int getLectureId() {
        return lectureId;
    }

    public int getGroupId() {
        return groupId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        LectureGroup that = (LectureGroup) o;
        return id == that.id &&
                lectureId == that.lectureId &&
                groupId == that.groupId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, lectureId, groupId);
    }

    @Override
    public String toString() {
        return "LectureGroup{" +
                "id=" + id +
                ", lectureId=" + lectureId +
                ", groupId=" + groupId +
                '}';
    }
}

【问题讨论】:

  • 请出示您的GroupLectureGroup 实体。
  • 您遇到什么异常?请提供更多详细信息
  • @SternK 添加了它
  • @DimitrisBaltas 我只在百里香视图中使用此方法,所以我有 org.springframework.web.util.NestedServletException: Request processing failed;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式的异常:“groupService.getGroupsOnLecture(lecture.getId())”
  • @llya 你没有显示LectureGroup

标签: java sql spring hibernate hql


【解决方案1】:

尝试更改为以下。原因是您只能在存在关系的情况下加入(Group join LectureGroup),因为您的类没有与 ManyToOne 等关系映射。您必须使用如下语法,或者您可以使用适当的关系映射更改您的类以使用您正在尝试的语法。

private static final String GET_ALL_GROUPS_FOR_LECTURE = "select distinct g FROM Group g, LectureGroup lg WHERE g.id = lg.groupId and lg.lectureId = :lectureId";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2013-07-02
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多