将 SQL 查询转换为 HQL 时出错,Java Hibernate
errror in converting SQL query to HQL, Java Hibernate
我有一个 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 and Group 和 LectureGroup Entites
@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 +
'}';
}
}
换成下面试试。原因是如果存在关系,您只能加入(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";
我有一个 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 and Group 和 LectureGroup Entites
@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 +
'}';
}
}
换成下面试试。原因是如果存在关系,您只能加入(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";