Bonjour,

je suis en train de configurer la couche d'accès aux données de mon appli et j'utilise JPA. J'ai trouvé un tutoriel qui explique comment faire ça ( http://java.dzone.com/articles/persi...er-spring-31-0 )

En gros il suffit de définir un Bean de configuration suivant
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 
package orm;
 
import javax.sql.DataSource;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
 
 
@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig{
 
	@Bean
	public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){
		final LocalContainerEntityManagerFactoryBean factoryBean
		= new LocalContainerEntityManagerFactoryBean();
		factoryBean.setDataSource( restDataSource() );
		factoryBean.setPackagesToScan( new String[ ] { "domain" } );
 
		final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(){
			{
				// JPA properties ...
			}
		};
		factoryBean.setJpaVendorAdapter( vendorAdapter );
		factoryBean.setJpaProperties( this.additionlProperties() );
 
		return factoryBean;
	}
 
	@Bean
	public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
		return new PersistenceExceptionTranslationPostProcessor();
	}
 
	@Bean
	public DataSource restDataSource(){
		final DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName( this.driverClassName );
		dataSource.setUrl( this.url );
		dataSource.setUsername( "restUser" );
		dataSource.setPassword( "restmy5ql" );
		return dataSource;
	}
 
	@Bean
	public PlatformTransactionManager transactionManager(){
		final JpaTransactionManager transactionManager = new JpaTransactionManager();
		transactionManager.setEntityManagerFactory(
				entityManagerFactoryBean().getObject() );
 
		return transactionManager;
	}
}

Mais je me demande comment l'utiliser ensuite. D'après ce que j'ai compris une fois ce Bean déclaré, je peux injecter l'entityManager (par introspection ?) comme ce qui suit (est-ce exact ?).

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package orm;

import java.io.Serializable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public abstract class AbstractJpaDAO< T extends Serializable > {

	private Class< T > clazz;

	@PersistenceContext
	EntityManager entityManager;

	public void delete( T entity ){
		this.entityManager.remove( entity );
	}

	public void deleteById( Long entityId ){
		final T entity = this.getById( entityId );

		this.delete( entity );
	}
	public List< T > findAll(){
		return this.entityManager.createQuery( "from " + this.clazz.getName() )
				.getResultList();
	}

	public T findOne( Long id ){
		return this.entityManager.find( this.clazz, id );
	}

	public void save( T entity ){
		this.entityManager.persist( entity );
	}

	public void setClazz( Class< T > clazzToSet ){
		this.clazz = clazzToSet;
	}
	public void update( T entity ){
		this.entityManager.merge( entity );
	}
}
Et je ne vois pas comment utiliser le PlatformTransactionManager et le PersistenceExceptionTranslationPostProcessor. D'autre part si je veux utiliser dans un fichier Spring xml ces 2 dernières propriété comment faire. Merci d'avance pour vos réponses