Skip to content

Instantly share code, notes, and snippets.

View eugenp's full-sized avatar

Eugen eugenp

View GitHub Profile
@eugenp
eugenp / PersistenceJPAConfig.java
Created December 21, 2011 22:10
The Persistence Layer with Spring Data JPA - The Persistence configuration
@Configuration
@EnableTransactionManagement
@ImportResource( "classpath*:*springDataConfig.xml" )
public class PersistenceJPAConfig{
...
}
@eugenp
eugenp / IFooDAO.java
Created December 21, 2011 19:48
The Persistence Layer with Spring Data JPA - the DAO
public interface IFooDAO extends JpaRepository< Foo, Long >{
Foo findByName( final String name );
}
@eugenp
eugenp / spring-data-config.xml
Created December 21, 2011 18:51
The Persistence Layer with Spring Data JPA - the Spring Data configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
@eugenp
eugenp / FooServiceIntegrationTest.java
Created December 21, 2011 17:38
The Persistence Layer with Spring Data JPA - the exception test
@Test( expected = DataAccessException.class )
public void whenAUniqueConstraintIsBroken_thenSpringSpecificExceptionIsThrown(){
String name = "randomName";
service.save( new Foo( name ) );
service.save( new Foo( name ) );
}
@eugenp
eugenp / pom.xml
Created December 12, 2011 20:32
The persistence layer with Spring 3.1 and JPA - the pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.9.Final</version>
<scope>runtime</scope>
@eugenp
eugenp / PersistenceJPAConfig.java
Created December 10, 2011 23:28
The persistence layer with Spring 3.1 and JPA - the Java Spring Configuration
@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig{
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){
LocalContainerEntityManagerFactoryBean factoryBean
= new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource( restDataSource() );
factoryBean.setPackagesToScan( new String[ ] { "org.rest" } );
@eugenp
eugenp / persistenceJpaConfig.xml
Created December 10, 2011 23:23
The persistence layer with Spring 3.1 and JPA - the XML configuration
<bean id="myEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.rest" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${hibernate.show_sql}" />
<property name="generateDdl" value="${jpa.generateDdl}" />
<property name="databasePlatform" value="${persistence.dialect}" />
</bean>
@eugenp
eugenp / GenericDAO.java
Created December 7, 2011 20:48
Simplyfing the DAO layer with Spring and Java Generics - the Generic DAO implementations
@Repository
@Scope( BeanDefinition.SCOPE_PROTOTYPE )
public class GenericJpaDAO< T extends Serializable >
extends AbstractJpaDAO< T > implements IGenericDAO< T >{
//
}
@Repository
@Scope( BeanDefinition.SCOPE_PROTOTYPE )
public class GenericHibernateDAO< T extends Serializable >
@eugenp
eugenp / FooService.java
Created December 7, 2011 20:12
Simplyfing the DAO layer with Spring and Java Generics - the Service
@Service
class FooService implements IFooService{
IGenericDAO< Foo > dao;
@Autowired
public final void setDao( final IGenericDAO< Foo > daoToSet ){
dao = daoToSet;
dao.setClazz( Foo.class );
}
@eugenp
eugenp / AbstractJpaDAO.java
Created December 7, 2011 20:02
Simplyfing the DAO layer with Spring and Java Generics - the Abstract JPA DAO
public abstract class AbstractJpaDAO< T extends Serializable > {
private Class< T > clazz;
@PersistenceContext
EntityManager entityManager;
public void setClazz( final Class< T > clazzToSet ){
this.clazz = clazzToSet;
}