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
This commit is contained in:
parent
1757625dba
commit
864c2e2190
|
@ -141,7 +141,7 @@
|
|||
<!-- persistence -->
|
||||
<hibernate.version>3.6.10.Final</hibernate.version>
|
||||
<mysql-connector-java.version>5.1.40</mysql-connector-java.version>
|
||||
<tomcat-dbcp.version>7.0.73</tomcat-dbcp.version>
|
||||
<tomcat-dbcp.version>8.5.8</tomcat-dbcp.version>
|
||||
<h2.version>1.4.193</h2.version>
|
||||
|
||||
<!-- various -->
|
||||
|
@ -162,4 +162,4 @@
|
|||
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -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<Event> implements IEventDao {
|
||||
|
||||
public EventDao() {
|
||||
super();
|
||||
|
||||
setClazz(Event.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
|
||||
|
||||
|
||||
public interface IEventDao extends IOperations<Event> {
|
||||
//
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<mapping class="org.baeldung.persistence.model.Event" />
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
|
@ -0,0 +1,58 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:jee="http://www.springframework.org/schema/jee"
|
||||
xmlns:lang="http://www.springframework.org/schema/lang"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<context:annotation-config />
|
||||
<context:component-scan base-package="org.baeldung.persistence" />
|
||||
<context:property-placeholder location="classpath:persistence-mysql.properties"/>
|
||||
|
||||
|
||||
|
||||
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
||||
<property name="url" value="${jdbc.url}"/>
|
||||
<property name="username" value="${jdbc.user}"/>
|
||||
<property name="password" value="${jdbc.pass}"/>
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="sessionFactory"
|
||||
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="configLocation">
|
||||
<value>classpath:exceptionDemo.cfg.xml</value>
|
||||
</property>
|
||||
<property name="hibernateProperties">
|
||||
<props>
|
||||
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
|
||||
<prop key="hibernate.show_sql">true</prop>
|
||||
<prop key="hibernate.hbm2ddl.auto">create</prop>
|
||||
<!-- <prop key="hibernate.current_session_context_class">thread</prop>-->
|
||||
</props>
|
||||
</property>
|
||||
|
||||
</bean>
|
||||
|
||||
<!-- <bean id="employeeDAO" class="com.howtodoinjava.dao.EmployeeDaoImpl"></bean> -->
|
||||
<!-- <bean id="employeeManager" class="com.howtodoinjava.service.EmployeeManagerImpl"></bean> -->
|
||||
|
||||
<tx:annotation-driven transaction-manager="transactionManager"/>
|
||||
<bean id="transactionManager"
|
||||
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
|
||||
<property name="sessionFactory" ref="sessionFactory" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
|
@ -18,7 +18,7 @@
|
|||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
|
||||
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
||||
<property name="url" value="${jdbc.url}"/>
|
||||
<property name="username" value="${jdbc.user}"/>
|
||||
|
@ -31,4 +31,4 @@
|
|||
|
||||
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||
|
||||
</beans>
|
||||
</beans>
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue