Skip to content

Commit f147b8b

Browse files
author
eugenp
committed
initial persistence work for the exception project - to reproduce data exceptions
1 parent 6c3a03f commit f147b8b

17 files changed

+411
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.ex.dataIntegrityviolationexception.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
7+
8+
@Configuration
9+
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause1")
10+
public class Cause1DataContextWithJavaConfig {
11+
12+
public Cause1DataContextWithJavaConfig() {
13+
super();
14+
}
15+
16+
// beans
17+
18+
@Bean
19+
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
20+
return new PersistenceExceptionTranslationPostProcessor();
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.ex.dataIntegrityviolationexception.spring;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause2")
8+
public class Cause2DataContextWithJavaConfig {
9+
10+
public Cause2DataContextWithJavaConfig() {
11+
super();
12+
}
13+
14+
// beans
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.ex.dataIntegrityviolationexception.spring;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause3")
8+
public class Cause3DataContextWithJavaConfig {
9+
10+
public Cause3DataContextWithJavaConfig() {
11+
super();
12+
}
13+
14+
// beans
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung.ex.dataIntegrityviolationexception;
2+
3+
import org.baeldung.ex.dataIntegrityviolationexception.spring.Cause1DataContextWithJavaConfig;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.test.context.ContextConfiguration;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
9+
10+
@RunWith(SpringJUnit4ClassRunner.class)
11+
@ContextConfiguration(classes = { Cause1DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
12+
public class Cause1DataIntegrityViolationExceptionIntegrationTest {
13+
14+
@Test
15+
public final void givenContextIsInitialized_thenNoException() {
16+
//
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung.ex.dataIntegrityviolationexception;
2+
3+
import org.baeldung.ex.dataIntegrityviolationexception.spring.Cause2DataContextWithJavaConfig;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.test.context.ContextConfiguration;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
9+
10+
@RunWith(SpringJUnit4ClassRunner.class)
11+
@ContextConfiguration(classes = { Cause2DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
12+
public class Cause2DataIntegrityViolationExceptionIntegrationTest {
13+
14+
@Test
15+
public final void givenContextIsInitialized_thenNoException() {
16+
//
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung.ex.dataIntegrityviolationexception;
2+
3+
import org.baeldung.ex.dataIntegrityviolationexception.spring.Cause3DataContextWithJavaConfig;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.test.context.ContextConfiguration;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
9+
10+
@RunWith(SpringJUnit4ClassRunner.class)
11+
@ContextConfiguration(classes = { Cause3DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
12+
public class Cause3DataIntegrityViolationExceptionIntegrationTest {
13+
14+
@Test
15+
public final void givenContextIsInitialized_thenNoException() {
16+
//
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package org.baeldung.ex.nosuchbeandefinitionexception;
22

33
import org.baeldung.ex.nosuchbeandefinitionexception.spring.Cause1ContextWithJavaConfig;
4+
import org.baeldung.persistence.model.Child;
5+
import org.baeldung.persistence.model.Parent;
6+
import org.baeldung.persistence.service.IChildService;
7+
import org.baeldung.persistence.service.IParentService;
48
import org.junit.Test;
59
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.dao.DataIntegrityViolationException;
612
import org.springframework.test.context.ContextConfiguration;
713
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
814
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@@ -11,9 +17,23 @@
1117
@ContextConfiguration(classes = { Cause1ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
1218
public class Cause1NoSuchBeanDefinitionExceptionIntegrationTest {
1319

14-
@Test
15-
public final void givenContextIsInitialized_thenNoException() {
16-
//
20+
@Autowired
21+
private IParentService service;
22+
23+
@Autowired
24+
private IChildService childService;
25+
26+
// tests
27+
28+
@Test(expected = DataIntegrityViolationException.class)
29+
public void whenChildIsDeletedWhileParentStillHasForeignKeyToIt_thenDataException() {
30+
final Child childEntity = new Child();
31+
childService.create(childEntity);
32+
33+
final Parent parentEntity = new Parent(childEntity);
34+
service.create(parentEntity);
35+
36+
childService.delete(childEntity);
1737
}
1838

1939
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.baeldung.persistence.dao;
2+
3+
import org.baeldung.persistence.dao.common.IOperations;
4+
import org.baeldung.persistence.model.Child;
5+
6+
public interface IChildDao extends IOperations<Child> {
7+
//
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.baeldung.persistence.dao;
2+
3+
import org.baeldung.persistence.dao.common.IOperations;
4+
import org.baeldung.persistence.model.Parent;
5+
6+
public interface IParentDao extends IOperations<Parent> {
7+
//
8+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.baeldung.persistence.dao.common;
2+
3+
import java.io.Serializable;
4+
import java.util.List;
5+
6+
import org.hibernate.Session;
7+
import org.hibernate.SessionFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
10+
import com.google.common.base.Preconditions;
11+
12+
@SuppressWarnings("unchecked")
13+
public abstract class AbstractHibernateDao<T extends Serializable> implements IOperations<T> {
14+
private Class<T> clazz;
15+
16+
@Autowired
17+
private SessionFactory sessionFactory;
18+
19+
// API
20+
21+
protected final void setClazz(final Class<T> clazzToSet) {
22+
clazz = Preconditions.checkNotNull(clazzToSet);
23+
}
24+
25+
@Override
26+
public final T findOne(final long id) {
27+
return (T) getCurrentSession().get(clazz, id);
28+
}
29+
30+
@Override
31+
public final List<T> findAll() {
32+
return getCurrentSession().createQuery("from " + clazz.getName()).list();
33+
}
34+
35+
@Override
36+
public final void create(final T entity) {
37+
Preconditions.checkNotNull(entity);
38+
// getCurrentSession().persist(entity);
39+
getCurrentSession().saveOrUpdate(entity);
40+
}
41+
42+
@Override
43+
public final T update(final T entity) {
44+
Preconditions.checkNotNull(entity);
45+
return (T) getCurrentSession().merge(entity);
46+
}
47+
48+
@Override
49+
public final void delete(final T entity) {
50+
Preconditions.checkNotNull(entity);
51+
getCurrentSession().delete(entity);
52+
}
53+
54+
@Override
55+
public final void deleteById(final long entityId) {
56+
final T entity = findOne(entityId);
57+
Preconditions.checkState(entity != null);
58+
delete(entity);
59+
}
60+
61+
protected final Session getCurrentSession() {
62+
return sessionFactory.getCurrentSession();
63+
}
64+
65+
}

0 commit comments

Comments
 (0)