Criteria API 已弃用,如何改为转换 ti CriteriaQuery?
Criteria API deprecated, how to convert ti CriteriaQuery instead?
我正在将一个旧的现有项目迁移到 Springboot。
我有以下方法:
public List<T> findAll(Class<?> clazz) {
Criteria search = entityManager.unwrap(Session.class).createCriteria(clazz);
List<T> entities = search.list();
return entities;
}
还有一个:
public County findByName(String name) {
Criteria search = entityManager.unwrap(Session.class).createCriteria(County.class).add(Restrictions.eq("county", name));
County county = (County) search.uniqueResult();
return county;
}
这有效,但是当我 运行 项目时,我收到以下警告:Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA javax.persistence.criteria.CriteriaQuery instead
如何将其转换为使用 javax.persistence.criteria.CriteriaQuery
并仍然能够收到相同的结果?我从未使用过这两种 API。
感谢帮助!
查找全部:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Country> cq = cb.createQuery(Country.class);
Root<Country> rootCountry= cq.from(Country.class);
cq.select(rootCountry);
List<Country> result = entityManager.createQuery(cq).getResultList();
按名称查找:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Country> cq = cb.createQuery(Country.class);
Root<Country> rootCountry= cq.from(Country.class);
cq.select(rootCountry);
cq.where(cb.equal(rootCountry.get(Country_.name),NAME))
// if you don't use metamodel (although it is recommended) -> cq.where(cb.equal(rootCountry.get("name"),NAME))
Country result = entityManager.createQuery(cq).getSingleResult();
你能在这个 link https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/querycriteria.html
中阅读更多相关信息吗
我正在将一个旧的现有项目迁移到 Springboot。 我有以下方法:
public List<T> findAll(Class<?> clazz) {
Criteria search = entityManager.unwrap(Session.class).createCriteria(clazz);
List<T> entities = search.list();
return entities;
}
还有一个:
public County findByName(String name) {
Criteria search = entityManager.unwrap(Session.class).createCriteria(County.class).add(Restrictions.eq("county", name));
County county = (County) search.uniqueResult();
return county;
}
这有效,但是当我 运行 项目时,我收到以下警告:Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA javax.persistence.criteria.CriteriaQuery instead
如何将其转换为使用 javax.persistence.criteria.CriteriaQuery
并仍然能够收到相同的结果?我从未使用过这两种 API。
感谢帮助!
查找全部:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Country> cq = cb.createQuery(Country.class);
Root<Country> rootCountry= cq.from(Country.class);
cq.select(rootCountry);
List<Country> result = entityManager.createQuery(cq).getResultList();
按名称查找:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Country> cq = cb.createQuery(Country.class);
Root<Country> rootCountry= cq.from(Country.class);
cq.select(rootCountry);
cq.where(cb.equal(rootCountry.get(Country_.name),NAME))
// if you don't use metamodel (although it is recommended) -> cq.where(cb.equal(rootCountry.get("name"),NAME))
Country result = entityManager.createQuery(cq).getSingleResult();
你能在这个 link https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/querycriteria.html
中阅读更多相关信息吗