Bonjour,
J'ai le DAO suivant :
Le service 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 public class UserDAO extends HibernateDaoSupport implements IUserDAO{ /** * Log.<br/> */ private Log log = LogFactory.getLog(UserDAO.class); public UserDAO() { } /** * */ public void create(UserPersistanceBean user) throws PersistanceException { try { Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession(); session.save(user); log.info(new StringBuffer("Creation de l'utilisateur numero ").append(user.getIdUser()).toString()); } catch (Exception e) { log.error(e, e); throw new PersistanceException(e); } } /** * */ public UserPersistanceBean loadUserByLogin(String login) throws PersistanceException{ UserPersistanceBean userPersistance = null; try{ Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession(); String requete = RequeteRessources.getRequeteRessources().getSQLRequest("loadUserByLogin"); Query query = session.createQuery(requete); query.setParameter("login",login); List<UserPersistanceBean> listResult = query.list(); if (listResult != null){ if (listResult.size() > 1){ throw new PersistanceException("Il y'a un utilisateur pour le login suivant"); } if (listResult.size() == 1){ userPersistance = listResult.get(0); } } }catch(Exception e){ log.error(e,e); throw new PersistanceException(e); } return userPersistance; } }
et le fichier applicationContext.xml 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 public class UserBusinessService implements IUserBusinessService { protected Log log = LogFactory.getLog(IUserBusinessService.class); /** * UserDAO.<br/> */ private IUserDAO userDAO; /** * RoleDAO.<br/> */ private IRoleDAO roleDAO; public void createUser(UserBusinessBean user) throws LoginDejaExistantException, RoleNonExistantException, TechnicalException { try { // Conversion user business en user persistance UserPersistanceBean userPersistance = UserHelper.getUserPersistanceFromBusiness(user); // Verification que le login n'existe pas deja UserPersistanceBean userPersiByLog = userDAO.loadUserByLogin(userPersistance.getLogin()); if (userPersiByLog != null) { throw new LoginDejaExistantException("Le login existe deja."); } // Verification que le role existe vraiment RolePersistanceBean rolePersistance = userPersistance.getRole(); if (((rolePersistance != null) && (rolePersistance.getLabelRole() != null) && (!rolePersistance.getLabelRole().equals("")))) { RolePersistanceBean rolePersistanceRecup = roleDAO.loadRoleByLabel(rolePersistance.getLabelRole()); if (rolePersistanceRecup == null) { throw new RoleNonExistantException("Le role de l'utilisateur n'existe pas."); } userPersistance.setRole(rolePersistanceRecup); } // Creation de l'utilisateur. userDAO.create(userPersistance); // Envoi d'un email de confirmation. //this.sendEMailConfirmationCreationUser(user); } catch (RoleNonExistantException e) { log.info(e, e); throw e; } catch (LoginDejaExistantException e) { log.info(e, e); throw e; } catch (Exception e) { log.error(e, e); throw new TechnicalException(e); } // TODO : Send mail. }
Aprés avoir exposé mon code, j'aurai quelques questions (Je me suis servi de tutorial pour le fichier applicationContext.xml) :
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- DataSource --> <bean id="livreDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:/livreDb</value> </property> </bean> <bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="livreDataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.connection.autocommit">false</prop> </props> </property> <property name="mappingResources"> <list> <value>fr/livre/persistance/user/User.hbm.xml</value> <value>fr/livre/persistance/user/Role.hbm.xml</value> </list> </property> </bean> <!-- DAO --> <bean id="userDAO" class="fr.livre.persistance.dao.user.UserDAO"> <property name="sessionFactory"> <ref local="sessionFactoryBean" /> </property> </bean> <bean id="roleDAO" class="fr.livre.persistance.dao.user.RoleDAO"> <property name="sessionFactory"> <ref local="sessionFactoryBean" /> </property> </bean> <!-- Couche metier --> <bean id="userServiceTarget" class="fr.livre.business.user.UserBusinessService"> <property name="userDAO"> <ref local="userDAO" /> </property> <property name="roleDAO"> <ref local="roleDAO" /> </property> </bean> <!-- Gestionnaire de transaction. --> <bean id="serviceManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="livreDataSource" /> </property> </bean> <!-- Service avec support de transaction --> <bean name="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="serviceManager" /> </property> <property name="target"> <ref local="userServiceTarget" /> </property> <property name="transactionAttributes"> <props> <prop key="createUser">PROPAGATION_REQUIRED, -fr.livre.business.exception.LoginDejaExistantException, -fr.livre.business.exception.RoleNonExistantException, -fr.livre.business.exception.TechnicalException </prop> </props> </property> </bean> </beans>
1 - Dans le fichier applicationContext.xml, la datasource est crée par spring il me semble. Peux t'on se servir de la datasource crée par le serveur application (Qui je suppose est plus efficace). Si oui, comment peux t'on faire?
2 - J'aimerai gérer les transactions. J'avais fait une première version avec les transactions et hibernate et ceux ci étaient bien gérés. Je suis passé à spring et hibernate et justement j'ai eu envie de gérer aussi les transactions
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 <bean id="livreDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:/livreDb</value> </property> </bean>. Malheureusement, celui ci commite tout le temps même en cas d'exception(
).
- J'ai pensé mettre autocommit à false mais la c'est le contraire. Ils commitent jamais.
Je vous remercie de votre aide (Autre que google est ton ami).
Koko22
Partager