gpt4 book ai didi

hibernate - 如何从 postgresql 数据库获取最新记录?

转载 作者:行者123 更新时间:2023-11-29 13:53:16 25 4
gpt4 key购买 nike

我正在使用 Hibernate+ postgreql 数据库。我还使用 session 进行 CRUD 操作。我成功完成了插入操作。我需要知道如何从实体获取最新记录。

 @Repository
@Transactional
public class IncidentsDAOImpl extends BaseDAO implements IncidentsDAO{
@Override
public Incidents getLatestRecord(String roleId) {
Incidents obj=new Incidents ();
List<Incidents> incidents = null;
incidents = (List<Incidents >) getSession()
.createCriteria(Incidents .class)
.add(Restrictions.eq("roleId", roleId))
.add(<how to add lastupdate query---------------------->);
obj=(incidents != null && !incidents .isEmpty()) ? incidents
.get(0) : null;
return obj;
}

如何添加 lastupdate 查询?

最佳答案

试试这个例子:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
String myDate = "17-04-2011";
// Create date 17-04-2011 - 00h00
Date minDate = formatter.parse(myDate);
// Create date 18-04-2011 - 00h00
// -> We take the 1st date and add it 1 day in millisecond thanks to a useful and not so known class
Date maxDate = new Date(minDate.getTime() + TimeUnit.DAYS.toMillis(1));
Conjunction and = Restrictions.conjunction();
// The order date must be >= 17-04-2011 - 00h00
and.add( Restrictions.ge("orderDate", minDate) );
// And the order date must be < 18-04-2011 - 00h00
and.add( Restrictions.lt("orderDate", maxDate) );

来源:Hibernate Criteria for Dates

关于hibernate - 如何从 postgresql 数据库获取最新记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37073401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com