hibernate.iを使って最新のオブジェクトを見つけるのに過去3時間の間頭を悩ませていましたが、 null、またはcastTypeExcetpionhibernateのlast_modifiedフィールドに基づいて最新のレコードを取得する方法JPA(セッションインプリメンテーション)
public Content getMostRecentByCategoryAndGenre(Category category, Genre genre){
logger.info("calling getMostRecentByCategoryAndGenre");
logger.debug(category);
logger.debug(genre);
DetachedCriteria recentDate = DetachedCriteria.forClass(this.getPersistentClass());
recentDate.setProjection(Projections.max("lastModified"));
Content recent = (Content) this.getSessionFactory().getCurrentSession().createCriteria(this.getPersistentClass())
.add(Restrictions.eq("genre", genre))
.add(Restrictions.eq("category", category))
.setProjection(Projections.max("lastModified")) // gives cannot cast timestamp to com.bla.bla.bla.myproject
// .add(Property.forName("lastModified").eq(recentDate)) // this if uncommented and previous line commented gives null result
.uniqueResult();
logger.debug(recent);
return recent;
}
私は正しく行っていないのですか?これを読んでくれてありがとう。
クエリ1で、あなたは他のソリューションのplease.Projectionに私を指すfeedback.can遅れて申し訳ありませんが、コンテンツIDを投射することができ、私は –
自分で行うことができ、最も簡単なの一つでありますlastModifiedの代わりに。そのIDを使用して、Contentオブジェクトを読み込むことができます。 – Pokuri