Skip to content

Commit cc7775d

Browse files
author
eugenp
committed
further persistence work
1 parent f6e6b97 commit cc7775d

7 files changed

Lines changed: 177 additions & 20 deletions

File tree

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.common.IOperations;
4+
import org.baeldung.persistence.model.Foo;
5+
6+
public interface IFooDao extends IOperations<Foo> {
7+
//
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.persistence.dao.impl;
2+
3+
import org.baeldung.persistence.common.AbstractHibernateDao;
4+
import org.baeldung.persistence.dao.IFooDao;
5+
import org.baeldung.persistence.model.Foo;
6+
import org.hibernate.SessionFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Repository;
9+
10+
@Repository
11+
public class FooDao extends AbstractHibernateDao<Foo> implements IFooDao {
12+
13+
@Autowired
14+
private SessionFactory sessionFactory;
15+
16+
public FooDao() {
17+
super();
18+
19+
setClazz(Foo.class);
20+
}
21+
22+
// API
23+
24+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.baeldung.persistence.model;
2+
3+
import java.io.Serializable;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
11+
@Entity
12+
public class Foo implements Serializable {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.AUTO)
16+
private long id;
17+
18+
@Column(nullable = false)
19+
private String name;
20+
21+
public Foo() {
22+
super();
23+
}
24+
25+
public Foo(final String name) {
26+
super();
27+
28+
this.name = name;
29+
}
30+
31+
// API
32+
33+
public long getId() {
34+
return id;
35+
}
36+
37+
public void setId(final long id) {
38+
this.id = id;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(final String name) {
46+
this.name = name;
47+
}
48+
49+
//
50+
51+
@Override
52+
public int hashCode() {
53+
final int prime = 31;
54+
int result = 1;
55+
result = prime * result + ((name == null) ? 0 : name.hashCode());
56+
return result;
57+
}
58+
59+
@Override
60+
public boolean equals(final Object obj) {
61+
if (this == obj)
62+
return true;
63+
if (obj == null)
64+
return false;
65+
if (getClass() != obj.getClass())
66+
return false;
67+
final Foo other = (Foo) obj;
68+
if (name == null) {
69+
if (other.name != null)
70+
return false;
71+
} else if (!name.equals(other.name))
72+
return false;
73+
return true;
74+
}
75+
76+
@Override
77+
public String toString() {
78+
final StringBuilder builder = new StringBuilder();
79+
builder.append("Foo [name=").append(name).append("]");
80+
return builder.toString();
81+
}
82+
83+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.baeldung.persistence.service;
2+
3+
import org.baeldung.persistence.common.IOperations;
4+
import org.baeldung.persistence.model.Foo;
5+
6+
public interface IFooService extends IOperations<Foo> {
7+
//
8+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.baeldung.persistence.service.impl;
2+
3+
import org.baeldung.persistence.common.AbstractService;
4+
import org.baeldung.persistence.common.IOperations;
5+
import org.baeldung.persistence.dao.IFooDao;
6+
import org.baeldung.persistence.model.Foo;
7+
import org.baeldung.persistence.service.IFooService;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
public class FooService extends AbstractService<Foo> implements IFooService {
13+
14+
@Autowired
15+
private IFooDao dao;
16+
17+
public FooService() {
18+
super();
19+
}
20+
21+
// API
22+
23+
@Override
24+
protected IOperations<Foo> getDao() {
25+
return dao;
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package org.baeldung.ex.dataIntegrityviolationexception;
22

33
import org.baeldung.ex.dataIntegrityviolationexception.spring.Cause1DataContextWithJavaConfig;
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 = { Cause1DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
1218
public class Cause1DataIntegrityViolationExceptionIntegrationTest {
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
}

‎spring-exceptions/src/test/java/org/baeldung/ex/nosuchbeandefinitionexception/Cause1NoSuchBeanDefinitionExceptionIntegrationTest.java‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,9 @@
1717
@ContextConfiguration(classes = { Cause1ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
1818
public class Cause1NoSuchBeanDefinitionExceptionIntegrationTest {
1919

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);
20+
@Test
21+
public final void givenContextIsInitialized_thenNoException() {
22+
//
3723
}
3824

3925
}

0 commit comments

Comments
 (0)