1

I have a entity, something like this:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity(name = "warranties")
public class Warranty implements Comparable<Warranty> {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "warranty_pk")
  private Long id;

  @Column(nullable = false)
  private Date startDate;

  @Column(nullable = false)
  private Date expireDate;

  @Column
  private String note;

  @Transient
  private Date displayDate;
  }
}

Notice that displayDate is annotated with @Transient.

I have two functionalities related to this entity, one is to fetch some of them, to do that I need to check expiration dates. So in my repository I have the following query:

  @Query(value =
      "SELECT "
          + "IF(expire_date < ((CURDATE() + INTERVAL 1 DAY) - INTERVAL 1 SECOND), expire_date, (expire_date - INTERVAL 90 day)) AS display_date, * "
          + "FROM warranties "
          + "WHERE expire_date < ((CURDATE() + INTERVAL 1 DAY) - INTERVAL 1 SECOND) + INTERVAL 90 DAY "
          + "AND expire_date <= :expire_date "
          + "AND start_date >= :startDate "
          + "AND expire_date <= :endDate + INTERVAL 90 DAY "
          + "ORDER BY display_date DESC",
      nativeQuery = true)
  List<Warranty> findWarranties(@Param("expire_date") Date expireDate,
      @Param("startDate") Date startDate, @Param("endDate") Date endDate);

Its that way so I can retrieve the "displayDate" calculated by the database.

It works fine when I use the following import:

import org.springframework.data.annotation.Transient;

And the other functionality is a save, where I save a given note.

For that case I get the following error:

Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'warranty0_.display_date' in 'field list'

If I use the following import, the second functionality works fine:

import javax.persistence.Transient;

But then the findWarranties doesn't return displayDate.

Any thoughts?

  • 2
    You need to use the one from `javax.persistence` which indicates basically that this isn't a DB field. Your issue is you are trying to shoehorn too much into a single object. For your query create a dedicated DTO instead of trying to shoehorn it into the entity. – M. Deinum Dec 14 '22 at 16:54
  • That worked!! Besides that I need an approach like this: https://stackoverflow.com/a/55487634/6047129 Thanks :) – Felipe Hogrefe Bento Dec 15 '22 at 13:17

0 Answers0