猿问

JPA 命名查询 findAllBy 与 Long 和 String

我对 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。


这是 ratingServiceImpl 中使用 @Autowired ratingRepository 的方法调用:


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>


收到一只叮咚
浏览 154回答 1
1回答

holdtom

创建 JPA 实体时,尽量不要使用 DB 保留字作为列名和变量。我相信问题出在@Column(name&nbsp;=&nbsp;"like") private&nbsp;Long&nbsp;like;spring生成的SQL语句是:...&nbsp;&nbsp;RATING0_.LIKE[*]&nbsp;AS&nbsp;LIKE5_48_,可以更改列名称吗?
随时随地看视频慕课网APP

相关分类

Java
我要回答