From 864c2e21901e8dd433320a663fd05a312d0a5982 Mon Sep 17 00:00:00 2001 From: Buddhini Samarakkody Date: Wed, 25 Oct 2017 18:07:36 +0530 Subject: [PATCH] Code changes added for BAEL-656 (#2698) * BAEL-656 - Add new DAO interface and Impl classes * BAEL-656 - Add new model class * BAEL-656 - Add new Service class * BAEL-656 - Add configuration files * BAEL-656 - maven upgrade for tomcat-dbcp version * BAEL-656 - Add JUnit Test classes * BAEL-656 - upgrade tomcat-dbcp version * BAEL-656 - for update of tomcat-dbcp version * Delete exceptionDemoPersistanceConfig.xml * BAEL-656 - Add renamed file * Update NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java * Update NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java --- spring-hibernate3/pom.xml | 4 +- .../baeldung/persistence/dao/EventDao.java | 18 ++++++ .../baeldung/persistence/dao/IEventDao.java | 9 +++ .../org/baeldung/persistence/model/Event.java | 45 ++++++++++++++ .../persistence/service/EventService.java | 27 +++++++++ .../baeldung/spring/PersistenceConfig.java | 4 +- .../src/main/resources/exceptionDemo.cfg.xml | 9 +++ .../exceptionDemoPersistenceConfig.xml | 58 +++++++++++++++++++ .../src/main/resources/persistenceConfig.xml | 4 +- ...ingAnnoSessionBeanMainIntegrationTest.java | 42 ++++++++++++++ ...ngLocalSessionBeanMainIntegrationTest.java | 39 +++++++++++++ 11 files changed, 253 insertions(+), 6 deletions(-) create mode 100644 spring-hibernate3/src/main/java/org/baeldung/persistence/dao/EventDao.java create mode 100644 spring-hibernate3/src/main/java/org/baeldung/persistence/dao/IEventDao.java create mode 100644 spring-hibernate3/src/main/java/org/baeldung/persistence/model/Event.java create mode 100644 spring-hibernate3/src/main/java/org/baeldung/persistence/service/EventService.java create mode 100644 spring-hibernate3/src/main/resources/exceptionDemo.cfg.xml create mode 100644 spring-hibernate3/src/main/resources/exceptionDemoPersistenceConfig.xml create mode 100644 spring-hibernate3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java create mode 100644 spring-hibernate3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java diff --git a/spring-hibernate3/pom.xml b/spring-hibernate3/pom.xml index d7607cac0a..c36b1c0522 100644 --- a/spring-hibernate3/pom.xml +++ b/spring-hibernate3/pom.xml @@ -141,7 +141,7 @@ 3.6.10.Final 5.1.40 - 7.0.73 + 8.5.8 1.4.193 @@ -162,4 +162,4 @@ - \ No newline at end of file + diff --git a/spring-hibernate3/src/main/java/org/baeldung/persistence/dao/EventDao.java b/spring-hibernate3/src/main/java/org/baeldung/persistence/dao/EventDao.java new file mode 100644 index 0000000000..74da643f35 --- /dev/null +++ b/spring-hibernate3/src/main/java/org/baeldung/persistence/dao/EventDao.java @@ -0,0 +1,18 @@ +package org.baeldung.persistence.dao; + + +import org.baeldung.persistence.model.Event; +import org.springframework.stereotype.Repository; + +@Repository +public class EventDao extends AbstractHibernateDao implements IEventDao { + + public EventDao() { + super(); + + setClazz(Event.class); + } + + // API + +} diff --git a/spring-hibernate3/src/main/java/org/baeldung/persistence/dao/IEventDao.java b/spring-hibernate3/src/main/java/org/baeldung/persistence/dao/IEventDao.java new file mode 100644 index 0000000000..f7be705905 --- /dev/null +++ b/spring-hibernate3/src/main/java/org/baeldung/persistence/dao/IEventDao.java @@ -0,0 +1,9 @@ +package org.baeldung.persistence.dao; + +import org.baeldung.persistence.model.Event; + + + +public interface IEventDao extends IOperations { + // +} diff --git a/spring-hibernate3/src/main/java/org/baeldung/persistence/model/Event.java b/spring-hibernate3/src/main/java/org/baeldung/persistence/model/Event.java new file mode 100644 index 0000000000..1d659ed75c --- /dev/null +++ b/spring-hibernate3/src/main/java/org/baeldung/persistence/model/Event.java @@ -0,0 +1,45 @@ +package org.baeldung.persistence.model; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "EVENTS") +public class Event implements Serializable { + + @Id + @GeneratedValue + private Long id; + + private String description; + + public Event() { + } + + + public Event(String description) { + this.description = description; + } + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + } \ No newline at end of file diff --git a/spring-hibernate3/src/main/java/org/baeldung/persistence/service/EventService.java b/spring-hibernate3/src/main/java/org/baeldung/persistence/service/EventService.java new file mode 100644 index 0000000000..6171751cc5 --- /dev/null +++ b/spring-hibernate3/src/main/java/org/baeldung/persistence/service/EventService.java @@ -0,0 +1,27 @@ +package org.baeldung.persistence.service; + + +import org.baeldung.persistence.dao.IEventDao; +import org.baeldung.persistence.model.Event; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class EventService { + + @Autowired + private IEventDao dao; + + public EventService() { + super(); + } + + // API + + public void create(final Event entity) { + dao.create(entity); + } + +} diff --git a/spring-hibernate3/src/main/java/org/baeldung/spring/PersistenceConfig.java b/spring-hibernate3/src/main/java/org/baeldung/spring/PersistenceConfig.java index fea76dfc70..03b9bfac33 100644 --- a/spring-hibernate3/src/main/java/org/baeldung/spring/PersistenceConfig.java +++ b/spring-hibernate3/src/main/java/org/baeldung/spring/PersistenceConfig.java @@ -4,7 +4,7 @@ import java.util.Properties; import javax.sql.DataSource; -import org.apache.tomcat.dbcp.dbcp.BasicDataSource; +import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -75,4 +75,4 @@ public class PersistenceConfig { return hibernateProperties; } -} \ No newline at end of file +} diff --git a/spring-hibernate3/src/main/resources/exceptionDemo.cfg.xml b/spring-hibernate3/src/main/resources/exceptionDemo.cfg.xml new file mode 100644 index 0000000000..8a710cc559 --- /dev/null +++ b/spring-hibernate3/src/main/resources/exceptionDemo.cfg.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/spring-hibernate3/src/main/resources/exceptionDemoPersistenceConfig.xml b/spring-hibernate3/src/main/resources/exceptionDemoPersistenceConfig.xml new file mode 100644 index 0000000000..09314c67b1 --- /dev/null +++ b/spring-hibernate3/src/main/resources/exceptionDemoPersistenceConfig.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + classpath:exceptionDemo.cfg.xml + + + + ${hibernate.dialect} + true + create + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-hibernate3/src/main/resources/persistenceConfig.xml b/spring-hibernate3/src/main/resources/persistenceConfig.xml index 347e85ee51..e7ef9ad765 100644 --- a/spring-hibernate3/src/main/resources/persistenceConfig.xml +++ b/spring-hibernate3/src/main/resources/persistenceConfig.xml @@ -18,7 +18,7 @@ - + @@ -31,4 +31,4 @@ - \ No newline at end of file + diff --git a/spring-hibernate3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java b/spring-hibernate3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java new file mode 100644 index 0000000000..2b29dcb7a9 --- /dev/null +++ b/spring-hibernate3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest.java @@ -0,0 +1,42 @@ +package org.baeldung.persistence.service; + +import org.baeldung.persistence.model.Event; +import org.baeldung.spring.PersistenceXmlConfig; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.orm.hibernate3.HibernateSystemException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceXmlConfig.class }, loader = AnnotationConfigContextLoader.class) +public class NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest { + + @Autowired + EventService service; + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public final void whenEntityIsCreated_thenNoExceptions() { + service.create(new Event("from Annotation Session Bean Factory")); + } + + @Test + @Ignore + public final void whenNoTransBoundToSession_thenException() { + expectedEx.expect(HibernateSystemException.class); + expectedEx.expectMessage("No Hibernate Session bound to thread, " + + "and configuration does not allow creation of " + + "non-transactional one here"); + service.create(new Event("from Annotation Session Bean Factory")); + } + +} diff --git a/spring-hibernate3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java b/spring-hibernate3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java new file mode 100644 index 0000000000..1bc6c07b18 --- /dev/null +++ b/spring-hibernate3/src/test/java/org/baeldung/persistence/service/NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest.java @@ -0,0 +1,39 @@ +package org.baeldung.persistence.service; + +import org.baeldung.persistence.model.Event; +import org.hibernate.HibernateException; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.orm.hibernate3.HibernateSystemException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath:exceptionDemoPersistenceConfig.xml" }) +public class NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest { + + @Autowired + EventService service; + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public final void whenEntityIsCreated_thenNoExceptions() { + service.create(new Event("from local session bean factory")); + } + + @Test + @Ignore + public final void whenNoTransBoundToSession_thenException() { + expectedEx.expect(HibernateException.class); + expectedEx.expectMessage("No Hibernate Session bound to thread, " + + "and configuration does not allow creation " + + "of non-transactional one here"); + service.create(new Event("from local session bean factory")); + } +}