persistence wortk
This commit is contained in:
parent
c7a1377cf6
commit
c82d342eae
|
@ -42,7 +42,7 @@ org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled
|
||||||
org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore
|
org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore
|
||||||
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore
|
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore
|
||||||
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
|
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
|
||||||
org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning
|
org.eclipse.jdt.core.compiler.problem.missingSerialVersion=ignore
|
||||||
org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore
|
org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore
|
||||||
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
|
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
|
||||||
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
|
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package org.baeldung.ex.mappingexception.cause1.persistence.dao;
|
||||||
|
|
||||||
|
import org.baeldung.ex.mappingexception.cause1.persistence.model.Foo;
|
||||||
|
import org.baeldung.persistence.common.AbstractHibernateDao;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class FooDao extends AbstractHibernateDao<Foo> implements IFooDao {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
public FooDao() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
setClazz(Foo.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package org.baeldung.ex.mappingexception.cause1.persistence.dao;
|
||||||
|
|
||||||
|
import org.baeldung.ex.mappingexception.cause1.persistence.model.Foo;
|
||||||
|
import org.baeldung.persistence.common.IOperations;
|
||||||
|
|
||||||
|
public interface IFooDao extends IOperations<Foo> {
|
||||||
|
//
|
||||||
|
}
|
|
@ -0,0 +1,81 @@
|
||||||
|
package org.baeldung.ex.mappingexception.cause1.persistence.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
public class Foo implements Serializable {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Foo() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Foo(final String name) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(final long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(final String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(final Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
final Foo other = (Foo) obj;
|
||||||
|
if (name == null) {
|
||||||
|
if (other.name != null)
|
||||||
|
return false;
|
||||||
|
} else if (!name.equals(other.name))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
final StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("Foo [name=").append(name).append("]");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package org.baeldung.ex.mappingexception.cause1.persistence.service;
|
||||||
|
|
||||||
|
import org.baeldung.ex.mappingexception.cause1.persistence.dao.IFooDao;
|
||||||
|
import org.baeldung.ex.mappingexception.cause1.persistence.model.Foo;
|
||||||
|
import org.baeldung.persistence.common.AbstractService;
|
||||||
|
import org.baeldung.persistence.common.IOperations;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FooService extends AbstractService<Foo> implements IFooService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IFooDao dao;
|
||||||
|
|
||||||
|
public FooService() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected IOperations<Foo> getDao() {
|
||||||
|
return dao;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package org.baeldung.ex.mappingexception.cause1.persistence.service;
|
||||||
|
|
||||||
|
import org.baeldung.ex.mappingexception.cause1.persistence.model.Foo;
|
||||||
|
import org.baeldung.persistence.common.IOperations;
|
||||||
|
|
||||||
|
public interface IFooService extends IOperations<Foo> {
|
||||||
|
//
|
||||||
|
}
|
|
@ -21,13 +21,13 @@ import com.google.common.base.Preconditions;
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
@PropertySource({ "classpath:persistence-mysql.properties" })
|
@PropertySource({ "classpath:persistence-mysql.properties" })
|
||||||
@ComponentScan({ "org.baeldung.persistence" })
|
@ComponentScan({ "org.baeldung.ex.mappingexception.cause1.persistence" })
|
||||||
public class PersistenceConfig {
|
public class Cause1PersistenceConfig {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment env;
|
private Environment env;
|
||||||
|
|
||||||
public PersistenceConfig() {
|
public Cause1PersistenceConfig() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ public class PersistenceConfig {
|
||||||
public LocalSessionFactoryBean sessionFactory() {
|
public LocalSessionFactoryBean sessionFactory() {
|
||||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||||
sessionFactory.setDataSource(restDataSource());
|
sessionFactory.setDataSource(restDataSource());
|
||||||
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model2" });
|
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.ex.mappingexception.cause1.persistence.model" });
|
||||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||||
|
|
||||||
return sessionFactory;
|
return sessionFactory;
|
|
@ -2,9 +2,9 @@ package org.baeldung.ex.mappingexception;
|
||||||
|
|
||||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
|
|
||||||
import org.baeldung.ex.mappingexception.spring.PersistenceConfig;
|
import org.baeldung.ex.mappingexception.cause1.persistence.model.Foo;
|
||||||
import org.baeldung.persistence.model.Foo;
|
import org.baeldung.ex.mappingexception.cause1.persistence.service.IFooService;
|
||||||
import org.baeldung.persistence.service.IFooService;
|
import org.baeldung.ex.mappingexception.spring.Cause1PersistenceConfig;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -13,17 +13,17 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
@ContextConfiguration(classes = { Cause1PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
public class MappingExceptionIntegrationTest {
|
public class Cause1MappingExceptionIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IFooService fooService;
|
private IFooService fooApi;
|
||||||
|
|
||||||
// tests
|
// tests
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenEntityIsPersisted_thenException() {
|
public final void givenEntityIsPersisted_thenException() {
|
||||||
fooService.create(new Foo(randomAlphabetic(6)));
|
fooApi.create(new Foo(randomAlphabetic(6)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue