我对 spring data JPA 命名方法 findAllBy 有问题...
这是我的实体:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@Column(name = "entity_id")
private Long entityId;
@Column(name = "entity_name")
private String entityName;
@Column(name = "user_id")
private Long userId;
@Column(name = "rating")
private Double rating;
@Column(name = "like")
private Long like;
@Column(name = "dislike")
private Long dislike;
@Column(name = "review_title")
private String reviewTitle;
@Lob
@Column(name = "review_comment")
private String reviewComment;
@Column(name = "time")
private ZonedDateTime time;
@ManyToOne
private RatingType type;
带有 getter 和 setter。
这是通过 @Autowired ratingRepository 在 ratingServiceImpl 中调用的方法:
List<Rating> ratings = ratingRepository.findAllByEntityIdAndEntityName(entityId, entityName);
和存储库:
@Repository
public interface RatingRepository extends JpaRepository<Rating, Long>,
JpaSpecificationExecutor<Rating> {
List<Rating> findAllByEntityIdAndEntityName(Long entityId, String entityName);
}
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
原因:
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT RATING0_.ID AS ID1_48_, RATING0_.DISLIKE AS DISLIKE2_48_, RATING0_.ENTITY_ID AS ENTITY_I3_48_, RATING0_.ENTITY_NAME AS ENTITY_N4_48_, RATING0_.LIKE[*] AS LIKE5_48_, RATING0_.RATING AS RATING6_48_, RATING0_.REVIEW_COMMENT AS REVIEW_C7_48_, RATING0_.REVIEW_TITLE AS REVIEW_T8_48_, RATING0_.TIME AS TIME9_48_, RATING0_.TYPE_ID AS TYPE_ID11_48_, RATING0_.USER_ID AS USER_ID10_48_ FROM RATING RATING0_ WHERE RATING0_.ENTITY_ID=? AND RATING0_.ENTITY_NAME=? "; expected "identifier"; SQL statement: select rating0_.id as id1_48_, rating0_.dislike as dislike2_48_, rating0_.entity_id as entity_i3_48_, rating0_.entity_name as entity_n4_48_, rating0_.like as like5_48_, rating0_.rating as rating6_48_, rating0_.review_comment as review_c7_48_, rating0_.review_title as review_t8_48_, rating0_.time as time9_48_, rating0_.type_id as type_id11_48_, rating0_.user_id as user_id10_48_ from rating rating0_ where rating0_.entity_id=? and rating0_.entity_name=? [42001-196]
other locations of exception on this link
感谢您提前提供解决方案。
最佳答案
创建 JPA 实体时,尽量不要使用数据库保留字作为列名和变量。
我相信问题出在
@Column(name = "like")
private Long like;
如spring生成的SQL语句为:
... RATING0_.LIKE[*] AS LIKE5_48_,
您可以更改列名称吗?
关于java - JPA 命名查询 findAllBy 与 Long 和 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58272054/