persistence wortk

This commit is contained in:
eugenp 2013-08-12 22:34:31 +03:00
parent c7a1377cf6
commit c82d342eae
8 changed files with 159 additions and 12 deletions

View File

@ -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.missingOverrideAnnotation=ignore
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.noEffectAssignment=warning
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning

View File

@ -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
}

View File

@ -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> {
//
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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> {
//
}

View File

@ -21,13 +21,13 @@ import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.persistence" })
public class PersistenceConfig {
@ComponentScan({ "org.baeldung.ex.mappingexception.cause1.persistence" })
public class Cause1PersistenceConfig {
@Autowired
private Environment env;
public PersistenceConfig() {
public Cause1PersistenceConfig() {
super();
}
@ -35,7 +35,7 @@ public class PersistenceConfig {
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
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());
return sessionFactory;

View File

@ -2,9 +2,9 @@ package org.baeldung.ex.mappingexception;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.baeldung.ex.mappingexception.spring.PersistenceConfig;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService;
import org.baeldung.ex.mappingexception.cause1.persistence.model.Foo;
import org.baeldung.ex.mappingexception.cause1.persistence.service.IFooService;
import org.baeldung.ex.mappingexception.spring.Cause1PersistenceConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
public class MappingExceptionIntegrationTest {
@ContextConfiguration(classes = { Cause1PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
public class Cause1MappingExceptionIntegrationTest {
@Autowired
private IFooService fooService;
private IFooService fooApi;
// tests
@Test
public final void givenEntityIsPersisted_thenException() {
fooService.create(new Foo(randomAlphabetic(6)));
fooApi.create(new Foo(randomAlphabetic(6)));
}
}