From c77925209d5f9fe9411c509bfb12118f75d729bc Mon Sep 17 00:00:00 2001 From: Robert Volkmann <20912167+robertvolkmann@users.noreply.github.com> Date: Sun, 6 Nov 2022 12:12:23 +0100 Subject: [PATCH] Revert "refactoring #1012: Remove deprecated method to ease Hibernate upgrade (#2125)" This reverts commit 9822f04d297bd2685f83f2550ee1e39ae4e8d3a7. --- .../servicelayer/common/DaoBaseImpl.java | 23 ++++++------------- .../servicelayer/spell/SpellDaoImpl.java | 15 ++++-------- .../spellbook/SpellbookDaoImpl.java | 15 ++++-------- .../servicelayer/wizard/WizardDaoImpl.java | 14 ++++------- .../magic/MagicServiceImplTest.java | 21 +++++++++-------- 5 files changed, 30 insertions(+), 58 deletions(-) diff --git a/service-layer/src/main/java/com/iluwatar/servicelayer/common/DaoBaseImpl.java b/service-layer/src/main/java/com/iluwatar/servicelayer/common/DaoBaseImpl.java index 46981c76ddc2..8c3415881a9b 100644 --- a/service-layer/src/main/java/com/iluwatar/servicelayer/common/DaoBaseImpl.java +++ b/service-layer/src/main/java/com/iluwatar/servicelayer/common/DaoBaseImpl.java @@ -27,12 +27,10 @@ import com.iluwatar.servicelayer.hibernate.HibernateUtil; import java.lang.reflect.ParameterizedType; import java.util.List; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; +import org.hibernate.Criteria; import org.hibernate.SessionFactory; import org.hibernate.Transaction; -import org.hibernate.query.Query; +import org.hibernate.criterion.Restrictions; /** * Base class for Dao implementations. @@ -59,12 +57,9 @@ public E find(Long id) { E result; try (var session = getSessionFactory().openSession()) { tx = session.beginTransaction(); - CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); - CriteriaQuery builderQuery = criteriaBuilder.createQuery(persistentClass); - Root root = builderQuery.from(persistentClass); - builderQuery.select(root).where(criteriaBuilder.equal(root.get("id"), id)); - Query query = session.createQuery(builderQuery); - result = query.uniqueResult(); + var criteria = session.createCriteria(persistentClass); + criteria.add(Restrictions.idEq(id)); + result = (E) criteria.uniqueResult(); tx.commit(); } catch (Exception e) { if (tx != null) { @@ -128,12 +123,8 @@ public List findAll() { List result; try (var session = getSessionFactory().openSession()) { tx = session.beginTransaction(); - CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); - CriteriaQuery builderQuery = criteriaBuilder.createQuery(persistentClass); - Root root = builderQuery.from(persistentClass); - builderQuery.select(root); - Query query = session.createQuery(builderQuery); - result = query.getResultList(); + Criteria criteria = session.createCriteria(persistentClass); + result = criteria.list(); } catch (Exception e) { if (tx != null) { tx.rollback(); diff --git a/service-layer/src/main/java/com/iluwatar/servicelayer/spell/SpellDaoImpl.java b/service-layer/src/main/java/com/iluwatar/servicelayer/spell/SpellDaoImpl.java index 5ee39f4be733..53de9b6ec241 100644 --- a/service-layer/src/main/java/com/iluwatar/servicelayer/spell/SpellDaoImpl.java +++ b/service-layer/src/main/java/com/iluwatar/servicelayer/spell/SpellDaoImpl.java @@ -25,12 +25,8 @@ package com.iluwatar.servicelayer.spell; import com.iluwatar.servicelayer.common.DaoBaseImpl; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; import org.hibernate.Transaction; -import org.hibernate.query.Query; - +import org.hibernate.criterion.Restrictions; /** * SpellDao implementation. @@ -43,12 +39,9 @@ public Spell findByName(String name) { Spell result; try (var session = getSessionFactory().openSession()) { tx = session.beginTransaction(); - CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); - CriteriaQuery builderQuery = criteriaBuilder.createQuery(Spell.class); - Root root = builderQuery.from(Spell.class); - builderQuery.select(root).where(criteriaBuilder.equal(root.get("name"), name)); - Query query = session.createQuery(builderQuery); - result = query.uniqueResult(); + var criteria = session.createCriteria(persistentClass); + criteria.add(Restrictions.eq("name", name)); + result = (Spell) criteria.uniqueResult(); tx.commit(); } catch (Exception e) { if (tx != null) { diff --git a/service-layer/src/main/java/com/iluwatar/servicelayer/spellbook/SpellbookDaoImpl.java b/service-layer/src/main/java/com/iluwatar/servicelayer/spellbook/SpellbookDaoImpl.java index 463b06cdd369..d68f7253e8ad 100644 --- a/service-layer/src/main/java/com/iluwatar/servicelayer/spellbook/SpellbookDaoImpl.java +++ b/service-layer/src/main/java/com/iluwatar/servicelayer/spellbook/SpellbookDaoImpl.java @@ -25,12 +25,8 @@ package com.iluwatar.servicelayer.spellbook; import com.iluwatar.servicelayer.common.DaoBaseImpl; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; import org.hibernate.Transaction; -import org.hibernate.query.Query; - +import org.hibernate.criterion.Restrictions; /** * SpellbookDao implementation. @@ -43,12 +39,9 @@ public Spellbook findByName(String name) { Spellbook result; try (var session = getSessionFactory().openSession()) { tx = session.beginTransaction(); - CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); - CriteriaQuery builderQuery = criteriaBuilder.createQuery(Spellbook.class); - Root root = builderQuery.from(Spellbook.class); - builderQuery.select(root).where(criteriaBuilder.equal(root.get("name"), name)); - Query query = session.createQuery(builderQuery); - result = query.uniqueResult(); + var criteria = session.createCriteria(persistentClass); + criteria.add(Restrictions.eq("name", name)); + result = (Spellbook) criteria.uniqueResult(); tx.commit(); } catch (Exception e) { if (tx != null) { diff --git a/service-layer/src/main/java/com/iluwatar/servicelayer/wizard/WizardDaoImpl.java b/service-layer/src/main/java/com/iluwatar/servicelayer/wizard/WizardDaoImpl.java index d35c9b0ca21b..043b96d5418b 100644 --- a/service-layer/src/main/java/com/iluwatar/servicelayer/wizard/WizardDaoImpl.java +++ b/service-layer/src/main/java/com/iluwatar/servicelayer/wizard/WizardDaoImpl.java @@ -25,11 +25,8 @@ package com.iluwatar.servicelayer.wizard; import com.iluwatar.servicelayer.common.DaoBaseImpl; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; import org.hibernate.Transaction; -import org.hibernate.query.Query; +import org.hibernate.criterion.Restrictions; /** * WizardDao implementation. @@ -42,12 +39,9 @@ public Wizard findByName(String name) { Wizard result; try (var session = getSessionFactory().openSession()) { tx = session.beginTransaction(); - CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); - CriteriaQuery builderQuery = criteriaBuilder.createQuery(Wizard.class); - Root root = builderQuery.from(Wizard.class); - builderQuery.select(root).where(criteriaBuilder.equal(root.get("name"), name)); - Query query = session.createQuery(builderQuery); - result = query.uniqueResult(); + var criteria = session.createCriteria(persistentClass); + criteria.add(Restrictions.eq("name", name)); + result = (Wizard) criteria.uniqueResult(); tx.commit(); } catch (Exception e) { if (tx != null) { diff --git a/service-layer/src/test/java/com/iluwatar/servicelayer/magic/MagicServiceImplTest.java b/service-layer/src/test/java/com/iluwatar/servicelayer/magic/MagicServiceImplTest.java index c145f8e5f978..491fd24467aa 100644 --- a/service-layer/src/test/java/com/iluwatar/servicelayer/magic/MagicServiceImplTest.java +++ b/service-layer/src/test/java/com/iluwatar/servicelayer/magic/MagicServiceImplTest.java @@ -26,10 +26,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; import com.iluwatar.servicelayer.spell.Spell; @@ -55,7 +56,7 @@ void testFindAllWizards() { final var spellDao = mock(SpellDao.class); final var service = new MagicServiceImpl(wizardDao, spellbookDao, spellDao); - verifyNoInteractions(wizardDao, spellbookDao, spellDao); + verifyZeroInteractions(wizardDao, spellbookDao, spellDao); service.findAllWizards(); verify(wizardDao).findAll(); @@ -69,7 +70,7 @@ void testFindAllSpellbooks() { final var spellDao = mock(SpellDao.class); final var service = new MagicServiceImpl(wizardDao, spellbookDao, spellDao); - verifyNoInteractions(wizardDao, spellbookDao, spellDao); + verifyZeroInteractions(wizardDao, spellbookDao, spellDao); service.findAllSpellbooks(); verify(spellbookDao).findAll(); @@ -83,7 +84,7 @@ void testFindAllSpells() { final var spellDao = mock(SpellDao.class); final var service = new MagicServiceImpl(wizardDao, spellbookDao, spellDao); - verifyNoInteractions(wizardDao, spellbookDao, spellDao); + verifyZeroInteractions(wizardDao, spellbookDao, spellDao); service.findAllSpells(); verify(spellDao).findAll(); @@ -102,17 +103,17 @@ void testFindWizardsWithSpellbook() { when(spellbook.getWizards()).thenReturn(wizards); final var spellbookDao = mock(SpellbookDao.class); - when(spellbookDao.findByName(bookname)).thenReturn(spellbook); + when(spellbookDao.findByName(eq(bookname))).thenReturn(spellbook); final var wizardDao = mock(WizardDao.class); final var spellDao = mock(SpellDao.class); final var service = new MagicServiceImpl(wizardDao, spellbookDao, spellDao); - verifyNoInteractions(wizardDao, spellbookDao, spellDao, spellbook); + verifyZeroInteractions(wizardDao, spellbookDao, spellDao, spellbook); final var result = service.findWizardsWithSpellbook(bookname); - verify(spellbookDao).findByName(bookname); + verify(spellbookDao).findByName(eq(bookname)); verify(spellbook).getWizards(); assertNotNull(result); @@ -139,13 +140,13 @@ void testFindWizardsWithSpell() throws Exception { final var spellName = "spellname"; final var spellDao = mock(SpellDao.class); - when(spellDao.findByName(spellName)).thenReturn(spell); + when(spellDao.findByName(eq(spellName))).thenReturn(spell); final var service = new MagicServiceImpl(wizardDao, spellbookDao, spellDao); - verifyNoInteractions(wizardDao, spellbookDao, spellDao, spellbook); + verifyZeroInteractions(wizardDao, spellbookDao, spellDao, spellbook); final var result = service.findWizardsWithSpell(spellName); - verify(spellDao).findByName(spellName); + verify(spellDao).findByName(eq(spellName)); verify(spellbook).getWizards(); assertNotNull(result);