Skip to content

Commit 2c1e978

Browse files
author
eugenp
committed
persistence workg
1 parent ec9b166 commit 2c1e978

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

spring-exceptions/src/main/resources/hibernate-mysql.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ hibernate.connection.username=tutorialuser
22
hibernate.connection.password=tutorialmy5ql
33
hibernate.connection.driver_class=com.mysql.jdbc.Driver
44
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
5-
#hibernate.connection.datasource=jdbc:mysql://localhost:3306/spring_hibernate4_exceptions?createDatabaseIfNotExist=true
65
hibernate.connection.url=jdbc:mysql://localhost:3306/spring_hibernate4_exceptions?createDatabaseIfNotExist=true
76
hibernate.show_sql=false
8-
hibernate.hbm2ddl.auto=create
7+
hibernate.hbm2ddl.auto=create

spring-exceptions/src/test/java/org/baeldung/ex/mappingexception/Cause4MappingExceptionIntegrationTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ public class Cause4MappingExceptionIntegrationTest {
1818

1919
@Test
2020
public final void givenEntityIsPersisted_thenException() throws IOException {
21-
final Configuration configuration = new Configuration();
21+
final SessionFactory sessionFactory = configureSessionFactory();
22+
23+
final Session session = sessionFactory.openSession();
24+
session.beginTransaction();
25+
session.saveOrUpdate(new Foo());
26+
session.getTransaction().commit();
27+
}
2228

29+
private final SessionFactory configureSessionFactory() throws IOException {
30+
final Configuration configuration = new Configuration();
2331
final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("hibernate-mysql.properties");
2432
final Properties hibernateProperties = new Properties();
2533
hibernateProperties.load(inputStream);
@@ -29,11 +37,7 @@ public final void givenEntityIsPersisted_thenException() throws IOException {
2937

3038
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
3139
final SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
32-
33-
final Session session = sessionFactory.openSession();
34-
session.beginTransaction();
35-
session.saveOrUpdate(new Foo());
36-
session.getTransaction().commit();
40+
return sessionFactory;
3741
}
3842

3943
}

0 commit comments

Comments
 (0)