From 2c1e9789a08b8a7fe1de10e77197b71081133eb3 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 13 Aug 2013 13:34:58 +0300 Subject: [PATCH] persistence workg --- .../main/resources/hibernate-mysql.properties | 3 +-- .../Cause4MappingExceptionIntegrationTest.java | 16 ++++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/spring-exceptions/src/main/resources/hibernate-mysql.properties b/spring-exceptions/src/main/resources/hibernate-mysql.properties index 5e8c7be141..49ca7fb3d1 100644 --- a/spring-exceptions/src/main/resources/hibernate-mysql.properties +++ b/spring-exceptions/src/main/resources/hibernate-mysql.properties @@ -2,7 +2,6 @@ hibernate.connection.username=tutorialuser hibernate.connection.password=tutorialmy5ql hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.dialect=org.hibernate.dialect.MySQL5Dialect -#hibernate.connection.datasource=jdbc:mysql://localhost:3306/spring_hibernate4_exceptions?createDatabaseIfNotExist=true hibernate.connection.url=jdbc:mysql://localhost:3306/spring_hibernate4_exceptions?createDatabaseIfNotExist=true hibernate.show_sql=false -hibernate.hbm2ddl.auto=create +hibernate.hbm2ddl.auto=create \ No newline at end of file diff --git a/spring-exceptions/src/test/java/org/baeldung/ex/mappingexception/Cause4MappingExceptionIntegrationTest.java b/spring-exceptions/src/test/java/org/baeldung/ex/mappingexception/Cause4MappingExceptionIntegrationTest.java index 37e624b16c..ea24a1342e 100644 --- a/spring-exceptions/src/test/java/org/baeldung/ex/mappingexception/Cause4MappingExceptionIntegrationTest.java +++ b/spring-exceptions/src/test/java/org/baeldung/ex/mappingexception/Cause4MappingExceptionIntegrationTest.java @@ -18,8 +18,16 @@ public class Cause4MappingExceptionIntegrationTest { @Test public final void givenEntityIsPersisted_thenException() throws IOException { - final Configuration configuration = new Configuration(); + final SessionFactory sessionFactory = configureSessionFactory(); + final Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.saveOrUpdate(new Foo()); + session.getTransaction().commit(); + } + + private final SessionFactory configureSessionFactory() throws IOException { + final Configuration configuration = new Configuration(); final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("hibernate-mysql.properties"); final Properties hibernateProperties = new Properties(); hibernateProperties.load(inputStream); @@ -29,11 +37,7 @@ public class Cause4MappingExceptionIntegrationTest { final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); final SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); - - final Session session = sessionFactory.openSession(); - session.beginTransaction(); - session.saveOrUpdate(new Foo()); - session.getTransaction().commit(); + return sessionFactory; } }