This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Configuration | |
| @EnableTransactionManagement | |
| @ImportResource( "classpath*:*springDataConfig.xml" ) | |
| public class PersistenceJPAConfig{ | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface IFooDAO extends JpaRepository< Foo, Long >{ | |
| Foo findByName( final String name ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Test( expected = DataAccessException.class ) | |
| public void whenAUniqueConstraintIsBroken_thenSpringSpecificExceptionIsThrown(){ | |
| String name = "randomName"; | |
| service.save( new Foo( name ) ); | |
| service.save( new Foo( name ) ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Configuration | |
| @EnableTransactionManagement | |
| public class PersistenceJPAConfig{ | |
| @Bean | |
| public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){ | |
| LocalContainerEntityManagerFactoryBean factoryBean | |
| = new LocalContainerEntityManagerFactoryBean(); | |
| factoryBean.setDataSource( restDataSource() ); | |
| factoryBean.setPackagesToScan( new String[ ] { "org.rest" } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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 > |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Service | |
| class FooService implements IFooService{ | |
| IGenericDAO< Foo > dao; | |
| @Autowired | |
| public final void setDao( final IGenericDAO< Foo > daoToSet ){ | |
| dao = daoToSet; | |
| dao.setClazz( Foo.class ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class AbstractJpaDAO< T extends Serializable > { | |
| private Class< T > clazz; | |
| @PersistenceContext | |
| EntityManager entityManager; | |
| public void setClazz( final Class< T > clazzToSet ){ | |
| this.clazz = clazzToSet; | |
| } |