0

I have a Spring project where I access the database using Spring Data REST (using http://spring.io/guides/gs/accessing-data-rest/)

@RepositoryRestResource(collectionResourceRel = "test", path = "test")
public interface TestRepository extends PagingAndSortingRepository<Test, Long> {

    @Query("SELECT max(p.lastUpdatedDate) FROM Test p")
    Date findLastUpdatedDate();
}

When I try to access the above method to get the MAX date using the URL localhost:8080/test/search/findLastUpdatedDate, I get the error

{"cause":null,"message":"Cannot create self link for class java.sql.Timestamp! No persistent entity found!"}

Please suggest how can I get the max lastUpdatedDate from the Test table. Thanks!

Here is my Test class:

@Entity
@Table(name="test")
public class Test implements Serializable{

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String col1;
  private String col2;
  private String status;

  @Column(name = "last_updated_date")
  private Date lastUpdatedDate;

  // getters, setters, hashcode, equals, toString methods are written
}
mona
  • 6,079
  • 12
  • 41
  • 46
  • Do you really need to return a Date type from the repository? Might avoid the error if you return a Test type and just read its lastUpdatedDate instead – Neil McGuigan Dec 03 '14 at 19:16
  • I needed a Date type from the repository (the output that we get when we run select max(last_updated_date) from Test: something like 2014-11-20). I tried to return a Test Type from the repository and since there are more that 1 row which have max date as 2014-11-20, ended up returning a List type. But all I wanted was the MAX date. There has to be an easier way to get the Date out from the repository... Not sure why HAL-JSON self link is giving this issue. I looked around in SO, but no luck... – mona Dec 03 '14 at 19:51

1 Answers1

0

You need to use the @Temporal annotation on dates.

You should also use java.util.Date or Joda time instead of java.sql.Timestamp

Spring Data JPA also has built-in created/modified timestamps, so you should look into that:

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#auditing

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • Thanks for your answer! When I changed it to JODA date, this is the error I get: {"cause":null,"message":"Cannot create self link for class org.joda.time.LocalDate! No persistent entity found!"} This error also shows when I have the repository return String object instead of Test object. So it seems like a Hal-Json unable to create self link. – mona Dec 03 '14 at 14:34