Hibernate 3 to 5 migration
Some notes and tips:
by Pseudo code below
1. 1.
Typical current code
getCurrentSession().createCriteria(BT.class)
.add(Restrictions.eq("replacedBT",
replaced))
.addOD(OD.desc("id")).list();
Will be replaced by
CriteriaQuery<BT> criteriaQuery =
getCurrentSession().getCriteriaBuilder().createQuery(BT.class);
Root<BT> root = criteriaQuery.from(BT.class);
CriteriaBuilder cb =
getCurrentSession().getCriteriaBuilder();
criteriaQuery.select(root).where(getCurrentSession().getCriteriaBuilder().equal(root.get("replacedBT"),
replaced));
criteriaQuery.ODBy(cb.desc(root.get("id")));
2.
Mapping
|
Hibernate
|
JPA
|
1
|
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
|
criteriaQuery.select(root).distinct(true)
|
2
|
setProjection(Property.forName("id")
|
criteriaQuery.multiselect(root.get("id"))
|
|
|
|
3
|
Disjunction
|
CriteriaBuilder disjunction();
Ex:
Predicate predicate =
cb.disjunction();
predicate.getExpressions().add(cb.isNull(root.get("effectiveEndDate")));
predicate.getExpressions().add(cb.greaterThanOrEqualTo(root.get("effectiveEndDate"),
DateUtil.getLienDateForTestY(testY.intValue())));
|
4
|
Criterion
|
predicate.getExpressions().add(
|
5
|
Restrictions.in ("testY",
Ys)
|
root.get("testY").in(Ys)
|
|
|
|
6
|
Restrictions.le
|
lessThanOrEqualTo
|
7
|
Restrictions.ilike - case insensitive
|
cb.like(cb.lower(root.get("assesseeName")),
"%"+name.trim().toLowerCase() + "%"
|
8
|
Build list of conditions
many if else
|
List<Predicate>
predicates = new ArrayList<>();
predicates.add(join.get("testId").in(oidSub));
predicates.add(cbSub.notEqual(rootBT.get("pmStatus"),
BTPmStatus.paidStatus));
predicates.add(cbSub.notEqual(rootBT.get("status"),
TestBTStatus.canceled));
predicates.add(cbSub.notEqual(rootBT.get("status"),
TestBTStatus.purged));
predicates.add(cbSub.notEqual(rootBT.get("status"),
TestBTStatus.rolledToUnsecured ));
|
9
|
Join
BTCrit.createCriteria("testCharges","chg");
BTCrit.add
(Restrictions.in("chg.testId", oidSub) );
|
Join<BT,
Charge> join = rootBT.join("testCharges", JoinType.INNER);
criteriaQueryBT.select(rootBT).where(join.get("testId").in(oids),
cbBT.equal(rootBT.get("testY"), testY),
rootBT.get("BTType").in(types) );
|
10
|
Multiple Join
List<JTestOD> chains =
getCurrentSession()
.createCriteria(JTestOD.class)
.add(Restrictions.not(Restrictions.eq("state",
JTestODState.open)))
.createCriteria("traTranss")
.createCriteria("JTransProperties")
.add(Restrictions.eq("id.apn",prop.getPropertyId()))
.add(Restrictions.eq("propertyAvTestY",prop.getTestY()))
// .add(Restrictions.eq("chainedToTestOD",
co))
// .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
// .list();
|
CriteriaQuery<JTestOD>
criteriaQuery = getCurrentSession().getCriteriaBuilder().createQuery(JTestOD.class);
CriteriaBuilder cb =
getCurrentSession().getCriteriaBuilder();
Root<JTestOD> root = criteriaQuery.from(JTestOD.class);
Join<JTestOD, JTraTrans> join =
root.join("traTranss", JoinType.INNER);
Join<JTraTrans, JTransProperty>
join2 = join.join("JTransProperties", JoinType.INNER);
criteriaQuery.select(root).distinct(true) .where(
cb.notEqual(root.get("state"),
JTestODState.open),
cb
.equal(join2.get("id").get("apn"), prop.getPropertyId()),
cb.equal(join2.get("propertyAvTestY"),
prop.getTestY()),
cb
.equal(join2.get("chainedToTestOD"), co));
List<JTestOD> chains =
getCurrentSession().createQuery(criteriaQuery).getResultList();
|