Correct use of @Transient

61 Views Asked by At

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?

0

There are 0 best solutions below