0

I've got problem with Spring @Query annotation. It returns me nothing from postgresql despite of that there are a lot of records. I suppose that it is because of the fact that in db is eg. section_id instead of section column. From method bellow I have all data.

@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);

However I need method like bellow which returns me Picture:

@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);

Here's my entity:

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;
}
Bob
  • 309
  • 2
  • 5
  • 17
  • Possibility 1 - Spring Data Projections. Possibility 2 - Hibernate allows for lazy loading of individual properties. https://vladmihalcea.com/how-to-lazy-load-entity-properties-with-hibernate/ – Alan Hay Dec 17 '18 at 12:32
  • And ask a question about your problem - which from the comments below is about addressing performance issue with a large field - not your supposed solution. https://en.wikipedia.org/wiki/XY_problem – Alan Hay Dec 17 '18 at 12:36
  • I tried to do Projections but doesnt' work well for me. Lazy loading is not the issue, because from some places I have to get picture without data field. In case of that I will get only thumbnail. – Bob Dec 17 '18 at 13:17
  • 1
    Neither your question or subsequent comments make any real sense. – Alan Hay Dec 17 '18 at 13:58

1 Answers1

0

Try something like :

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

Also, you will need to keep an empty constructor to your entity.

Oreste Viron
  • 3,592
  • 3
  • 22
  • 34
  • I don't want to get `private byte[] data;` field. It is too big and takes too much time. – Bob Dec 17 '18 at 10:14
  • 1
    Then, check for JPA projections : https://stackoverflow.com/questions/28322376/exclude-some-fields-of-spring-data-rest-resource – Oreste Viron Dec 17 '18 at 10:18