使用 postgresql 从 table 获取最新记录

get latest record from table using postgresql

我有 table 名员工如下,

id|name1|number
--|-----|------
01|test |100
02|test1|101
03|test2|102 

我正在使用 jpa 和 eclipselink 实现,数据库是 postgresql。 这里我的要求是我想使用 select 查询获取最新记录。任何人都可以建议查询以始终获取最新记录。 谢谢,

您可以使用以下查询从 mysql

中获取适当的结果
select * from employee order by id desc limit 1

您必须在实体中添加 updatedDate 字段 class。

So I'll like to know if is there a SIMPLE way around my problem, meaning having @PrePersist or @PreUpdate or even other workaround to set the lastModified field still using session

实体Class:

@Entity
@Table(name = "employee")
public class Employee {

    @Id
    @GeneratedValue
    private Long id;


    @Column(name = "name")  
    private String name;

    @Column(name = "number")  
    private Long number;

    @Column(name = "updated_at")  
    @Temporal(TemporalType.TIMESTAMP)  
    private Date updatedAt;  

    // getter setter

    @PreUpdate  
    public void setChangeDate() {  
       this.updatedAt = new Date();  
    }  

}

您可以使用JPA 查询。本次考试:

String query = "SELECT emp FROM Employee emp order by updatedAt desc limit 1 ";