File tree Expand file tree Collapse file tree
main/java/org/baeldung/ex/mappingexception
test/java/org/baeldung/ex/mappingexception Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled
4242org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod =ignore
4343org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation =ignore
4444org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation =enabled
45- org.eclipse.jdt.core.compiler.problem.missingSerialVersion =warning
45+ org.eclipse.jdt.core.compiler.problem.missingSerialVersion =ignore
4646org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod =ignore
4747org.eclipse.jdt.core.compiler.problem.noEffectAssignment =warning
4848org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion =warning
Original file line number Diff line number Diff line change 1+ package org .baeldung .ex .mappingexception .cause1 .persistence .dao ;
2+
3+ import org .baeldung .ex .mappingexception .cause1 .persistence .model .Foo ;
4+ import org .baeldung .persistence .common .AbstractHibernateDao ;
5+ import org .hibernate .SessionFactory ;
6+ import org .springframework .beans .factory .annotation .Autowired ;
7+ import org .springframework .stereotype .Repository ;
8+
9+ @ Repository
10+ public class FooDao extends AbstractHibernateDao <Foo > implements IFooDao {
11+
12+ @ Autowired
13+ private SessionFactory sessionFactory ;
14+
15+ public FooDao () {
16+ super ();
17+
18+ setClazz (Foo .class );
19+ }
20+
21+ // API
22+
23+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .ex .mappingexception .cause1 .persistence .dao ;
2+
3+ import org .baeldung .ex .mappingexception .cause1 .persistence .model .Foo ;
4+ import org .baeldung .persistence .common .IOperations ;
5+
6+ public interface IFooDao extends IOperations <Foo > {
7+ //
8+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .ex .mappingexception .cause1 .persistence .model ;
2+
3+ import java .io .Serializable ;
4+
5+ import javax .persistence .Column ;
6+ import javax .persistence .GeneratedValue ;
7+ import javax .persistence .GenerationType ;
8+ import javax .persistence .Id ;
9+
10+ public class Foo implements Serializable {
11+
12+ @ Id
13+ @ GeneratedValue (strategy = GenerationType .AUTO )
14+ private long id ;
15+
16+ @ Column (nullable = false )
17+ private String name ;
18+
19+ public Foo () {
20+ super ();
21+ }
22+
23+ public Foo (final String name ) {
24+ super ();
25+
26+ this .name = name ;
27+ }
28+
29+ // API
30+
31+ public long getId () {
32+ return id ;
33+ }
34+
35+ public void setId (final long id ) {
36+ this .id = id ;
37+ }
38+
39+ public String getName () {
40+ return name ;
41+ }
42+
43+ public void setName (final String name ) {
44+ this .name = name ;
45+ }
46+
47+ //
48+
49+ @ Override
50+ public int hashCode () {
51+ final int prime = 31 ;
52+ int result = 1 ;
53+ result = prime * result + ((name == null ) ? 0 : name .hashCode ());
54+ return result ;
55+ }
56+
57+ @ Override
58+ public boolean equals (final Object obj ) {
59+ if (this == obj )
60+ return true ;
61+ if (obj == null )
62+ return false ;
63+ if (getClass () != obj .getClass ())
64+ return false ;
65+ final Foo other = (Foo ) obj ;
66+ if (name == null ) {
67+ if (other .name != null )
68+ return false ;
69+ } else if (!name .equals (other .name ))
70+ return false ;
71+ return true ;
72+ }
73+
74+ @ Override
75+ public String toString () {
76+ final StringBuilder builder = new StringBuilder ();
77+ builder .append ("Foo [name=" ).append (name ).append ("]" );
78+ return builder .toString ();
79+ }
80+
81+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .ex .mappingexception .cause1 .persistence .service ;
2+
3+ import org .baeldung .ex .mappingexception .cause1 .persistence .dao .IFooDao ;
4+ import org .baeldung .ex .mappingexception .cause1 .persistence .model .Foo ;
5+ import org .baeldung .persistence .common .AbstractService ;
6+ import org .baeldung .persistence .common .IOperations ;
7+ import org .springframework .beans .factory .annotation .Autowired ;
8+ import org .springframework .stereotype .Service ;
9+
10+ @ Service
11+ public class FooService extends AbstractService <Foo > implements IFooService {
12+
13+ @ Autowired
14+ private IFooDao dao ;
15+
16+ public FooService () {
17+ super ();
18+ }
19+
20+ // API
21+
22+ @ Override
23+ protected IOperations <Foo > getDao () {
24+ return dao ;
25+ }
26+
27+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .ex .mappingexception .cause1 .persistence .service ;
2+
3+ import org .baeldung .ex .mappingexception .cause1 .persistence .model .Foo ;
4+ import org .baeldung .persistence .common .IOperations ;
5+
6+ public interface IFooService extends IOperations <Foo > {
7+ //
8+ }
Original file line number Diff line number Diff line change 2121@ Configuration
2222@ EnableTransactionManagement
2323@ PropertySource ({ "classpath:persistence-mysql.properties" })
24- @ ComponentScan ({ "org.baeldung.persistence" })
25- public class PersistenceConfig {
24+ @ ComponentScan ({ "org.baeldung.ex.mappingexception.cause1. persistence" })
25+ public class Cause1PersistenceConfig {
2626
2727 @ Autowired
2828 private Environment env ;
2929
30- public PersistenceConfig () {
30+ public Cause1PersistenceConfig () {
3131 super ();
3232 }
3333
3434 @ Bean
3535 public LocalSessionFactoryBean sessionFactory () {
3636 final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean ();
3737 sessionFactory .setDataSource (restDataSource ());
38- sessionFactory .setPackagesToScan (new String [] { "org.baeldung.persistence.model2 " });
38+ sessionFactory .setPackagesToScan (new String [] { "org.baeldung.ex.mappingexception.cause1. persistence.model " });
3939 sessionFactory .setHibernateProperties (hibernateProperties ());
4040
4141 return sessionFactory ;
Original file line number Diff line number Diff line change 22
33import static org .apache .commons .lang3 .RandomStringUtils .randomAlphabetic ;
44
5- import org .baeldung .ex .mappingexception .spring . PersistenceConfig ;
6- import org .baeldung .persistence .model . Foo ;
7- import org .baeldung .persistence . service . IFooService ;
5+ import org .baeldung .ex .mappingexception .cause1 . persistence . model . Foo ;
6+ import org .baeldung .ex . mappingexception . cause1 . persistence .service . IFooService ;
7+ import org .baeldung .ex . mappingexception . spring . Cause1PersistenceConfig ;
88import org .junit .Test ;
99import org .junit .runner .RunWith ;
1010import org .springframework .beans .factory .annotation .Autowired ;
1313import org .springframework .test .context .support .AnnotationConfigContextLoader ;
1414
1515@ RunWith (SpringJUnit4ClassRunner .class )
16- @ ContextConfiguration (classes = { PersistenceConfig .class }, loader = AnnotationConfigContextLoader .class )
17- public class MappingExceptionIntegrationTest {
16+ @ ContextConfiguration (classes = { Cause1PersistenceConfig .class }, loader = AnnotationConfigContextLoader .class )
17+ public class Cause1MappingExceptionIntegrationTest {
1818
1919 @ Autowired
20- private IFooService fooService ;
20+ private IFooService fooApi ;
2121
2222 // tests
2323
2424 @ Test
2525 public final void givenEntityIsPersisted_thenException () {
26- fooService .create (new Foo (randomAlphabetic (6 )));
26+ fooApi .create (new Foo (randomAlphabetic (6 )));
2727 }
2828
2929}
You can’t perform that action at this time.
0 commit comments