使用@Formula(休眠注释)时无法提取结果集
Could not extract ResultSet when @Formula (hibernate annotation) is used
在我的应用程序中有 courses
和 course_ratings
table(我使用 MySQL)。
course_ratings
table 是 courses
table 的外键和一个名为 rating
的列,即 INT。
我将在示例中展示我的问题:
当我从数据库下载 ID 为 1 的课程时,我想从 course_ratings
中获取所有行,其中 courses
table 的外键为 1,然后计算 [= 的平均值所有这些行中的 17=] 列。
为此,我决定使用 @Formula
注释
看看我的实体:
@Entity
@Table(name = "courses")
public class Course {
@Id
@GeneratedValue(generator = "inc")
@GenericGenerator(name = "inc", strategy = "increment")
private int courseId;
@NotBlank(message = "Add the course's title!")
private String title;
private String description;
@Formula("SELECT AVG(cr.rating) FROM course_ratings cr WHERE cr.course_id = courseId")
private double averageRating;
@OneToMany(mappedBy = "course")
private Set<CourseRating> ratings;
}
@Formula
中的这个 SQL 是正确的,但是在从 JpaRepository 调用方法 findById(Integer id)
之后我得到这个错误:
2021-07-14 20:26:26.314 WARN 15268 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
2021-07-14 20:26:26.314 ERROR 15268 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : (conn=248) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT AVG(cr.rating) FROM course_ratings cr WHERE cr.course_id = 1 as formul...' at line 1
2021-07-14 20:26:26.323 INFO 15268 --- [nio-8080-exec-3] o.h.e.internal.DefaultLoadEventListener : HHH000327: Error performing load command
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
编辑:
我不知道它是否重要,但我已将 average_rating 列添加为 DECIMAL
:
ALTER TABLE courses ADD COLUMN average_rating DECIMAL(3, 2);
这是我的有效解决方案!
@Formula("SELECT AVG(cr.rating) FROM course_ratings cr WHERE cr.course_id = course_id")
我已将 courseId
更改为 course_id
,因为它是原始的 SQL,而不是 JPQL
在我的应用程序中有 courses
和 course_ratings
table(我使用 MySQL)。
course_ratings
table 是 courses
table 的外键和一个名为 rating
的列,即 INT。
我将在示例中展示我的问题:
当我从数据库下载 ID 为 1 的课程时,我想从 course_ratings
中获取所有行,其中 courses
table 的外键为 1,然后计算 [= 的平均值所有这些行中的 17=] 列。
为此,我决定使用 @Formula
注释
看看我的实体:
@Entity
@Table(name = "courses")
public class Course {
@Id
@GeneratedValue(generator = "inc")
@GenericGenerator(name = "inc", strategy = "increment")
private int courseId;
@NotBlank(message = "Add the course's title!")
private String title;
private String description;
@Formula("SELECT AVG(cr.rating) FROM course_ratings cr WHERE cr.course_id = courseId")
private double averageRating;
@OneToMany(mappedBy = "course")
private Set<CourseRating> ratings;
}
@Formula
中的这个 SQL 是正确的,但是在从 JpaRepository 调用方法 findById(Integer id)
之后我得到这个错误:
2021-07-14 20:26:26.314 WARN 15268 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
2021-07-14 20:26:26.314 ERROR 15268 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : (conn=248) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT AVG(cr.rating) FROM course_ratings cr WHERE cr.course_id = 1 as formul...' at line 1
2021-07-14 20:26:26.323 INFO 15268 --- [nio-8080-exec-3] o.h.e.internal.DefaultLoadEventListener : HHH000327: Error performing load command
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
编辑:
我不知道它是否重要,但我已将 average_rating 列添加为 DECIMAL
:
ALTER TABLE courses ADD COLUMN average_rating DECIMAL(3, 2);
这是我的有效解决方案!
@Formula("SELECT AVG(cr.rating) FROM course_ratings cr WHERE cr.course_id = course_id")
我已将 courseId
更改为 course_id
,因为它是原始的 SQL,而不是 JPQL