initial persistence work for the exception project - to reproduce data exceptions
This commit is contained in:
parent
6c3a03f929
commit
f147b8b0e1
|
@ -0,0 +1,23 @@
|
|||
package org.baeldung.ex.dataIntegrityviolationexception.spring;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause1")
|
||||
public class Cause1DataContextWithJavaConfig {
|
||||
|
||||
public Cause1DataContextWithJavaConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
// beans
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package org.baeldung.ex.dataIntegrityviolationexception.spring;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause2")
|
||||
public class Cause2DataContextWithJavaConfig {
|
||||
|
||||
public Cause2DataContextWithJavaConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
// beans
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package org.baeldung.ex.dataIntegrityviolationexception.spring;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause3")
|
||||
public class Cause3DataContextWithJavaConfig {
|
||||
|
||||
public Cause3DataContextWithJavaConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
// beans
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package org.baeldung.ex.dataIntegrityviolationexception;
|
||||
|
||||
import org.baeldung.ex.dataIntegrityviolationexception.spring.Cause1DataContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause1DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause1DataIntegrityViolationExceptionIntegrationTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package org.baeldung.ex.dataIntegrityviolationexception;
|
||||
|
||||
import org.baeldung.ex.dataIntegrityviolationexception.spring.Cause2DataContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause2DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause2DataIntegrityViolationExceptionIntegrationTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package org.baeldung.ex.dataIntegrityviolationexception;
|
||||
|
||||
import org.baeldung.ex.dataIntegrityviolationexception.spring.Cause3DataContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause3DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause3DataIntegrityViolationExceptionIntegrationTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
|
@ -1,8 +1,14 @@
|
|||
package org.baeldung.ex.nosuchbeandefinitionexception;
|
||||
|
||||
import org.baeldung.ex.nosuchbeandefinitionexception.spring.Cause1ContextWithJavaConfig;
|
||||
import org.baeldung.persistence.model.Child;
|
||||
import org.baeldung.persistence.model.Parent;
|
||||
import org.baeldung.persistence.service.IChildService;
|
||||
import org.baeldung.persistence.service.IParentService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
@ -11,9 +17,23 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
|||
@ContextConfiguration(classes = { Cause1ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause1NoSuchBeanDefinitionExceptionIntegrationTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
@Autowired
|
||||
private IParentService service;
|
||||
|
||||
@Autowired
|
||||
private IChildService childService;
|
||||
|
||||
// tests
|
||||
|
||||
@Test(expected = DataIntegrityViolationException.class)
|
||||
public void whenChildIsDeletedWhileParentStillHasForeignKeyToIt_thenDataException() {
|
||||
final Child childEntity = new Child();
|
||||
childService.create(childEntity);
|
||||
|
||||
final Parent parentEntity = new Parent(childEntity);
|
||||
service.create(parentEntity);
|
||||
|
||||
childService.delete(childEntity);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.dao.common.IOperations;
|
||||
import org.baeldung.persistence.model.Child;
|
||||
|
||||
public interface IChildDao extends IOperations<Child> {
|
||||
//
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.dao.common.IOperations;
|
||||
import org.baeldung.persistence.model.Parent;
|
||||
|
||||
public interface IParentDao extends IOperations<Parent> {
|
||||
//
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package org.baeldung.persistence.dao.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public abstract class AbstractHibernateDao<T extends Serializable> implements IOperations<T> {
|
||||
private Class<T> clazz;
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
// API
|
||||
|
||||
protected final void setClazz(final Class<T> clazzToSet) {
|
||||
clazz = Preconditions.checkNotNull(clazzToSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final T findOne(final long id) {
|
||||
return (T) getCurrentSession().get(clazz, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<T> findAll() {
|
||||
return getCurrentSession().createQuery("from " + clazz.getName()).list();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void create(final T entity) {
|
||||
Preconditions.checkNotNull(entity);
|
||||
// getCurrentSession().persist(entity);
|
||||
getCurrentSession().saveOrUpdate(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final T update(final T entity) {
|
||||
Preconditions.checkNotNull(entity);
|
||||
return (T) getCurrentSession().merge(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void delete(final T entity) {
|
||||
Preconditions.checkNotNull(entity);
|
||||
getCurrentSession().delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void deleteById(final long entityId) {
|
||||
final T entity = findOne(entityId);
|
||||
Preconditions.checkState(entity != null);
|
||||
delete(entity);
|
||||
}
|
||||
|
||||
protected final Session getCurrentSession() {
|
||||
return sessionFactory.getCurrentSession();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package org.baeldung.persistence.dao.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
public interface IOperations<T extends Serializable> {
|
||||
|
||||
T findOne(final long id);
|
||||
|
||||
List<T> findAll();
|
||||
|
||||
void create(final T entity);
|
||||
|
||||
T update(final T entity);
|
||||
|
||||
void delete(final T entity);
|
||||
|
||||
void deleteById(final long entityId);
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package org.baeldung.persistence.dao.impl;
|
||||
|
||||
import org.baeldung.persistence.dao.IChildDao;
|
||||
import org.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||
import org.baeldung.persistence.model.Child;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class ChildDao extends AbstractHibernateDao<Child> implements IChildDao {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
public ChildDao() {
|
||||
super();
|
||||
|
||||
setClazz(Child.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package org.baeldung.persistence.dao.impl;
|
||||
|
||||
import org.baeldung.persistence.dao.IParentDao;
|
||||
import org.baeldung.persistence.dao.common.AbstractHibernateDao;
|
||||
import org.baeldung.persistence.model.Parent;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class ParentDao extends AbstractHibernateDao<Parent> implements IParentDao {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
public ParentDao() {
|
||||
super();
|
||||
|
||||
setClazz(Parent.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package org.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class Child implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToOne(mappedBy = "child")
|
||||
private Parent parent;
|
||||
|
||||
public Child() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Parent getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(final Parent parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Child [id=").append(id).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package org.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
@Entity
|
||||
public class Parent implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH })
|
||||
@JoinColumn(name = "child_fk")
|
||||
private Child child;
|
||||
|
||||
public Parent() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Parent(final Child child) {
|
||||
super();
|
||||
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Child getChild() {
|
||||
return child;
|
||||
}
|
||||
|
||||
public void setChild(final Child child) {
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Parent [id=").append(id).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.dao.common.IOperations;
|
||||
import org.baeldung.persistence.model.Child;
|
||||
|
||||
public interface IChildService extends IOperations<Child> {
|
||||
//
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.dao.common.IOperations;
|
||||
import org.baeldung.persistence.model.Parent;
|
||||
|
||||
public interface IParentService extends IOperations<Parent> {
|
||||
//
|
||||
}
|
Loading…
Reference in New Issue