More code changes for BAEL-656 (#2900)
* Java Config file for LocalSessionFactoryBean * Remove test class * Remove test class * Add new test classes for BAEL-656 * remove unused spring config BAEL-656 * modification in test method * change in test method * change in test method
This commit is contained in:
parent
1e13b57895
commit
7a2d2c5fb0
@ -0,0 +1,81 @@
|
||||
package org.baeldung.spring;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.orm.hibernate3.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||
@ComponentScan({ "org.baeldung.persistence.dao", "org.baeldung.persistence.service" })
|
||||
public class PersistenceConfigHibernate3 {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public PersistenceConfigHibernate3() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalSessionFactoryBean sessionFactory() {
|
||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||
Resource config = new ClassPathResource("exceptionDemo.cfg.xml");
|
||||
sessionFactory.setDataSource(dataSource());
|
||||
sessionFactory.setConfigLocation(config);
|
||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
final BasicDataSource dataSource = new BasicDataSource();
|
||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Autowired
|
||||
public HibernateTransactionManager transactionManager(final SessionFactory sessionFactory) {
|
||||
final HibernateTransactionManager txManager = new HibernateTransactionManager();
|
||||
txManager.setSessionFactory(sessionFactory);
|
||||
|
||||
return txManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
final Properties hibernateProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||
return hibernateProperties;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package org.baeldung.persistence.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.baeldung.spring.PersistenceConfigHibernate3;
|
||||
import org.hamcrest.core.IsInstanceOf;
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfigHibernate3.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class HibernateExceptionScen1MainIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
EventService service;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedEx = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public final void whenEntityIsCreated_thenNoExceptions() {
|
||||
service.create(new Event("from LocalSessionFactoryBean"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public final void whenNoTransBoundToSession_thenException() {
|
||||
expectedEx.expectCause(IsInstanceOf.<Throwable>instanceOf(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 LocalSessionFactoryBean"));
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package org.baeldung.persistence.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.hamcrest.core.IsInstanceOf;
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class HibernateExceptionScen2MainIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
EventService service;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedEx = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public final void whenEntityIsCreated_thenNoExceptions() {
|
||||
service.create(new Event("from AnnotationSessionFactoryBean"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public final void whenNoTransBoundToSession_thenException() {
|
||||
expectedEx.expectCause(IsInstanceOf.<Throwable>instanceOf(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 AnnotationSessionFactoryBean"));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user