persistence cleanup

This commit is contained in:
eugenp 2014-01-25 15:28:38 +02:00
parent 0d5f88e206
commit 29f02a2a8b
4 changed files with 21 additions and 7 deletions

View File

@ -1,4 +1,4 @@
package org.baeldung.spring; package org.baeldung.config;
import java.util.Properties; import java.util.Properties;
@ -8,6 +8,7 @@ import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
@ -20,7 +21,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions; 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.persistence" })
@ -33,6 +34,8 @@ public class PersistenceJPAConfig {
super(); super();
} }
// beans
@Bean @Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() { public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
@ -40,7 +43,6 @@ public class PersistenceJPAConfig {
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
// vendorAdapter.set
em.setJpaVendorAdapter(vendorAdapter); em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties()); em.setJpaProperties(additionalProperties());

View File

@ -1,11 +1,10 @@
package org.baeldung.spring; package org.baeldung.config;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.ImportResource;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration // @Configuration
@EnableTransactionManagement @EnableTransactionManagement
@ComponentScan({ "org.baeldung.persistence" }) @ComponentScan({ "org.baeldung.persistence" })
@ImportResource({ "classpath:jpaConfig.xml" }) @ImportResource({ "classpath:jpaConfig.xml" })

View File

@ -23,4 +23,8 @@ public class FooService {
dao.create(entity); dao.create(entity);
} }
public Foo findOne(final long id) {
return dao.findOne(id);
}
} }

View File

@ -2,8 +2,9 @@ package org.baeldung.persistence.service;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.baeldung.config.PersistenceJPAConfig;
import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.model.Foo;
import org.baeldung.spring.PersistenceJPAConfig; import org.junit.Assert;
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;
@ -55,4 +56,12 @@ public class FooServicePersistenceIntegrationTest {
service.create(new Foo()); service.create(new Foo());
} }
@Test
public final void whenEntityIsCreated_thenFound() {
final Foo fooEntity = new Foo("abc");
service.create(fooEntity);
final Foo found = service.findOne(fooEntity.getId());
Assert.assertNotNull(found);
}
} }