Wierd Spring JPA behavior when using @Query

Viewed 50

There is something strange with @Query annotation right now I'm trying to retrieve some specific fields from a table using a list of users as an input:

@Query(value = "select new document(documentId, documentType, status, rejected) " + "from document where user in ?1") 
List<Document> findByUserInWithoutFiles(List<User> users);

The code above works as expected but now I need the user that owns that document so I added it to the constructor:

@Query(value = "select new document(documentId, documentType, status, rejected, user) " + "from document where user in ?1")
List<Document> findByUserInWithoutFiles(List<User> users);

But here throws

java.sql.SQLSyntaxErrorException: Unknown column 'user' in 'where clause'

Here is the Document Object.

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "document")
@Table(name = "document", schema = "foo")
public class Document implements Serializable {

  private static final long serialVersionUID = -9793924073464282L;

  @GeneratedValue(generator = "uuid2")
  @GenericGenerator(name = "uuid2", strategy = "uuid2")
  @Column(columnDefinition = "BINARY(16)", name = "document_id")
  @Id
  private UUID documentId;
  @Column(name = "document_type")
  private PhotoEnum documentType;
  @Column(name = "created_date")
  private LocalDateTime createdDate;
  @Column(name = "update_date")
  private LocalDateTime updateDate;
  @Lob
  @Column(name = "file", columnDefinition = "BLOB")
  private byte[] file;
  @Column(name = "status")
  private String status;
  @Column(name = "rejected")
  private String rejected;
  @Column(name = "expiration_date")
  private LocalDate expirationDate;
  @ManyToOne(targetEntity = User.class, fetch = FetchType.LAZY)
  @JoinColumn(name = "user_id")
  private User user;

  public Document(PhotoEnum documentType) {
    this.documentType = documentType;
  }

  public Document(UUID documentId, PhotoEnum documentType, String status, String rejected,
      User user) {
    this.documentId = documentId;
    this.documentType = documentType;
    this.status = status;
    this.rejected = rejected;
    this.user = user;
    }

So the question is what I'm doing wrong? is this a bug?

Is there another way to get partial information from this object? (I don't need the file field)

I would appreciate any advice.

Cheers!

EDIT: I have done some alternatives research and I ended up using Interface-based Projections instead of @Query annotation is a better and elegant solution.

1 Answers
Related