0

我有一个 Spring 项目,我使用 Spring Data REST 访问数据库(使用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();
}

当我尝试使用 URL localhost:8080/test/search/findLastUpdatedDate 访问上述方法以获取 MAX 日期时,出现错误

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

请建议我如何从测试表中获取最大 lastUpdatedDate。谢谢!

这是我的测试课:

@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
}
4

1 回答 1

0

您需要在日期上使用 @Temporal 注释。

您还应该使用 java.util.Date 或 Joda 时间而不是 java.sql.Timestamp

Spring Data JPA 还具有内置的创建/修改时间戳,因此您应该研究一下:

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

于 2014-12-02T21:28:15.790 回答