Skip to content
This repository was archived by the owner on Oct 26, 2023. It is now read-only.

Commit 9a2ba39

Browse files
committed
JPA - Polished configuration and test case.
1 parent e97e929 commit 9a2ba39

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
lines changed

jpa/src/main/java/com/oreilly/springdata/jpa/ApplicationConfig.java

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.orm.jpa.JpaDialect;
2828
import org.springframework.orm.jpa.JpaTransactionManager;
2929
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
30+
import org.springframework.orm.jpa.vendor.Database;
3031
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
3132
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
3233
import org.springframework.transaction.PlatformTransactionManager;
@@ -35,7 +36,7 @@
3536
/**
3637
* Spring JavaConfig configuration class to setup a Spring container and infrastructure components like a
3738
* {@link DataSource}, a {@link EntityManagerFactory} and a {@link PlatformTransactionManager}.
38-
*
39+
*
3940
* @author Oliver Gierke
4041
*/
4142
@Configuration
@@ -44,50 +45,53 @@
4445
@EnableTransactionManagement
4546
class ApplicationConfig {
4647

47-
/**
48-
* Bootstraps an in-memory HSQL database.
49-
*
50-
* @return
51-
* @see http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/jdbc.html#jdbc-embedded-database-support
52-
*/
53-
@Bean
54-
public DataSource dataSource() {
55-
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
56-
return builder.setType( EmbeddedDatabaseType.HSQL ).build();
57-
}
48+
/**
49+
* Bootstraps an in-memory HSQL database.
50+
*
51+
* @return
52+
* @see http
53+
* ://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/jdbc.html#jdbc-embedded-database
54+
* -support
55+
*/
56+
@Bean
57+
public DataSource dataSource() {
58+
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
59+
return builder.setType(EmbeddedDatabaseType.HSQL).build();
60+
}
5861

59-
/**
60-
* Sets up a {@link LocalContainerEntityManagerFactoryBean} to use Hibernate. Activates picking up entities from the
61-
* project's base package.
62-
*
63-
* @return
64-
*/
65-
@Bean
66-
public EntityManagerFactory entityManagerFactory() {
62+
/**
63+
* Sets up a {@link LocalContainerEntityManagerFactoryBean} to use Hibernate. Activates picking up entities from the
64+
* project's base package.
65+
*
66+
* @return
67+
*/
68+
@Bean
69+
public EntityManagerFactory entityManagerFactory() {
6770

68-
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
69-
vendorAdapter.setGenerateDdl( true );
71+
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
72+
vendorAdapter.setDatabase(Database.HSQL);
73+
vendorAdapter.setGenerateDdl(true);
7074

71-
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
72-
factory.setJpaVendorAdapter( vendorAdapter );
73-
factory.setPackagesToScan( getClass().getPackage().getName() );
74-
factory.setDataSource( dataSource() );
75+
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
76+
factory.setJpaVendorAdapter(vendorAdapter);
77+
factory.setPackagesToScan(getClass().getPackage().getName());
78+
factory.setDataSource(dataSource());
7579

76-
factory.afterPropertiesSet();
80+
factory.afterPropertiesSet();
7781

78-
return factory.getObject();
79-
}
82+
return factory.getObject();
83+
}
8084

81-
@Bean
82-
public JpaDialect jpaDialect() {
83-
return new HibernateJpaDialect();
84-
}
85+
@Bean
86+
public JpaDialect jpaDialect() {
87+
return new HibernateJpaDialect();
88+
}
8589

86-
@Bean
87-
public PlatformTransactionManager transactionManager() {
90+
@Bean
91+
public PlatformTransactionManager transactionManager() {
8892

89-
JpaTransactionManager txManager = new JpaTransactionManager();
90-
txManager.setEntityManagerFactory( entityManagerFactory() );
91-
return txManager;
92-
}
93+
JpaTransactionManager txManager = new JpaTransactionManager();
94+
txManager.setEntityManagerFactory(entityManagerFactory());
95+
return txManager;
96+
}
9397
}

jpa/src/main/resources/META-INF/spring/application-context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
77
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
88
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
9-
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd
9+
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
1010
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
1111

1212
<jpa:repositories base-package="com.oreilly.springdata.jpa" />

jpa/src/test/java/com/oreilly/springdata/jpa/ApplicationConfigTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2424
import org.springframework.context.support.ClassPathXmlApplicationContext;
2525

26+
import com.oreilly.springdata.jpa.core.CustomerRepository;
27+
2628
/**
2729
* Test case bootstrapping both JavaConfig and XML configuration to validate configuration.
2830
*
@@ -35,12 +37,14 @@ public void bootstrapAppFromJavaConfig() {
3537

3638
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
3739
assertThat(context, is(notNullValue()));
40+
assertThat(context.getBean(CustomerRepository.class), is(notNullValue()));
3841
}
3942

4043
@Test
41-
public void bootsrapAppFromXml() {
44+
public void bootstrapAppFromXml() {
4245

4346
ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/application-context.xml");
4447
assertThat(context, is(notNullValue()));
48+
assertThat(context.getBean(CustomerRepository.class), is(notNullValue()));
4549
}
4650
}

0 commit comments

Comments
 (0)