Spring Query注解

时间:2018-12-17 09:48:17

标签: spring hibernate spring-data-jpa hql

我对Spring @Query注释有疑问。尽管有很多记录,但它从postgresql中什么也没给我返回。我想这是因为db中的事实。 section_id,而不是section列。 从方法下面,我有所有数据。

@Query("select p.id, p.dataContentType, p.changeDate, p.name, p.section.id, p.line.id, p.type.id, "
    + "p.status.id, p.changeUser.id, p.insertDate, p.deletedDate, p.recNum, p.actual, p.previous.id, p.thumbnail "
    + "from Picture p where p.actual=true")
Page<Object[]> findAllWithoutData(Pageable pageable);

但是我需要像波纹管这样的方法,该方法可以返回图片:

@Query("select new Picture(p.id, p.dataContentType, p.changeDate, p.name, p.section, p.line, p.type, "
        + "p.status, p.changeUser, p.insertDate, p.deletedDate, p.recNum, p.actual, p.previous, p.thumbnail) "
        + "from Picture p where p.actual=true")
Page<Picture> findAllWithoutData(Pageable pageable);

这是我的实体:

public class Picture implements Serializable {

private Long id;
private byte[] data;
private String dataContentType;
private ZonedDateTime changeDate;
private String name;
private Section section;
private Line line;
private DictionaryValue type;
private DictionaryValue status;
private User changeUser;
private ZonedDateTime insertDate;
private ZonedDateTime deletedDate;
private Long recNum;
private Boolean actual;
private Picture previous;
private byte[] thumbnail;

public Picture(Long id, String dataContentType, ZonedDateTime changeDate, String name, Section section,
        Line line, DictionaryValue type, DictionaryValue status, User changeUser, ZonedDateTime insertDate,
        ZonedDateTime deletedDate, Long recNum, Boolean actual, Picture previous, byte[] thumbnail) {
    this.id = id;
    this.dataContentType = dataContentType;
    this.changeDate = changeDate;
    this.name = name;
    this.section = section;
    this.line = line;
    this.type = type;
    this.status = status;
    this.changeUser = changeUser;
    this.insertDate = insertDate;
    this.deletedDate = deletedDate;
    this.recNum = recNum;
    this.actual = actual;
    this.previous = previous;
    this.thumbnail = thumbnail;
}

1 个答案:

答案 0 :(得分:0)

尝试类似的东西:

@Query("select p from Picture p where p.actual=true")
Page<Picture> findAllWithoutData(Pageable pageable);

此外,您将需要为您的实体保留一个空的构造函数。