Bonjour à tous!!
Alors voici mon problème, je commence à développer une application utilisant spring et hibernate (ainsi que tapestry mais c'est une autre histoire ^^) et j'ai quelques problèmes pour initialiser ma session :
Voici tout d'abord les fichiers de configs hibernate & spring :
config hibernate :
config spring :
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 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Configuration de la connexion à la base de données --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/applira</property> <property name="hibernate.connection.username"></property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <!-- Print SQL to stdout, format it nicely --> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="use_sql_comments">true</property> <property name="use_scrollable_resultset">true</property> <!-- Mapping des modèles avec la table --> <mapping class="com.applira.domain.model.Batch"/> <mapping class="com.applira.domain.model.BatchMsg"/> <mapping class="com.applira.domain.model.Bornes"/> <mapping class="com.applira.domain.model.Instances"/> <mapping class="com.applira.domain.model.IsoMsg"/> <mapping class="com.applira.domain.model.Serveurs"/> <mapping class="com.applira.domain.model.TypeIso"/> <mapping class="com.applira.domain.model.User"/> </session-factory> </hibernate-configuration>
Sachant qu'il ne s'agit pas d'une application web donc pas de web.xml (je compte faire une appli web plus tard en incluant un jar de la présente application ^^)
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 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <!-- Application context DAO layer --> <beans> <!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= --> <!-- Hibernate sessionFactory definition --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> </bean> <!-- Hibernate Transaction Manager Definition --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- ========================= Start of DAO DEFINITIONS ========================= --> <!-- proxy for DAO using generic DAO --> <bean id="proxyDAO" abstract="true"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- userDao definition --> <bean id="userDao" class="com.applira.dao.common.impl.UserDaoImpl" parent="proxyDAO"> <constructor-arg value="com.applira.domain.model.User" /> </bean> <!-- ========================= Start of SERVICE DEFINITIONS ========================= --> <!-- Transactional proxy for Services --> <bean id="proxyService" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <props> <prop key="find*">PROPAGATION_REQUIRED, readOnly</prop> <prop key="get*">PROPAGATION_REQUIRED, readOnly</prop> <prop key="*">PROPAGATION_REQUIRED, -java.lang.Exception</prop> </props> </property> </bean> </beans>
Et j'utilise pour les DAO une classe générique contenant les méthodes utiles :
Et la classe UserDaoImpl sur laquelle je me base pour lancer un test :
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 package com.applira.dao.common.Impl; import com.applira.dao.common.GenericDao; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.util.List; import org.hibernate.Criteria; import org.hibernate.LockMode; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Example; import org.springframework.orm.hibernate3.SessionFactoryUtils; public abstract class GenericDaoImpl<T, ID extends Serializable> implements GenericDao<T, ID> { private Class<T> persistentClass; private SessionFactory sessionFactory; @SuppressWarnings("unchecked") public GenericDaoImpl() { this.persistentClass = (Class<T>) ((ParameterizedType) getClass() .getGenericSuperclass()).getActualTypeArguments()[0]; } public void setSession(SessionFactory s) { this.sessionFactory = s; } protected Session getSession() { boolean allowCreate = true; return SessionFactoryUtils.getSession(sessionFactory, allowCreate); } public Class<T> getPersistentClass() { return persistentClass; } @SuppressWarnings("unchecked") public T findById(ID id, boolean lock) { T entity; if (lock) entity = (T) getSession().get(getPersistentClass(), id, LockMode.UPGRADE); else entity = (T) getSession().get(getPersistentClass(), id); return entity; } public List<T> findAll() { return findByCriteria(); } @SuppressWarnings("unchecked") public List<T> findByExample(T exampleInstance, String... excludeProperty) { Criteria crit = getSession().createCriteria(getPersistentClass()); Example example = Example.create(exampleInstance); for (String exclude : excludeProperty) { example.excludeProperty(exclude); } crit.add(example); return crit.list(); } public T makePersistent(T entity) { getSession().saveOrUpdate(entity); return entity; } public void makeTransient(T entity) { getSession().delete(entity); } public void flush() { getSession().flush(); } public void clear() { getSession().clear(); } /** * Use this inside subclasses as a convenience method. */ @SuppressWarnings("unchecked") protected List<T> findByCriteria(Criterion... criterion) { Criteria crit = getSession().createCriteria(getPersistentClass()); for (Criterion c : criterion) { crit.add(c); } return crit.list(); } }
Le problème est que lorsque je lance un test sur checkLogin (je vais voir en base si un user existe et que le mdp est bien celui attendu).. j'obtient ceci :
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 /** * */ package com.applira.dao.common.Impl; import org.hibernate.Criteria; import org.hibernate.criterion.Restrictions; import org.springframework.dao.DataAccessException; import org.apache.log4j.Logger; import com.applira.dao.common.UserDao; import com.applira.domain.model.User; /** * @author moi * */ public class UserDaoImpl extends GenericDaoImpl <User, Long> implements UserDao { private static Logger logger = Logger.getLogger(UserDaoImpl.class); public boolean checkLogin(String nomUser, String pwd) { try { logger.info("Check user with login: "+nomUser+" and password : [PROTECTED]"); Criteria crit = getSession().createCriteria(User.class); if (nomUser != null){ crit.add(Restrictions.ilike("nomUser", nomUser)); } crit.add(Restrictions.eq("pwd", pwd)); User user = (User)crit.uniqueResult(); return (user != null); } catch(DataAccessException e) { // Critical errors : database unreachable, etc. logger.error("Exception - DataAccessException occurs : "+e.getMessage() +" on complete checkLogin()."); return false; } } }
Sachant que j'utilise maven et junit pour ma classe de test...-------------------------------------------------------------------------------
Test set: com.atosworldine.effia.applira.common.test.UserDaoImplTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.157 sec <<< FAILURE!
testCheckLogin(com.applira.common.test.UserDaoImplTest) Time elapsed: 0.125 sec <<< ERROR!
java.lang.IllegalArgumentException: No SessionFactory specified
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:283)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:202)
at com.applira.dao.common.Impl.GenericDaoImpl.getSession(GenericDaoImpl.java:39)
at com.applira.dao.common.Impl.UserDaoImpl.checkLogin(UserDaoImpl.java:31)
at com.applira.common.test.UserDaoImplTest.testCheckLogin(UserDaoImplTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Le problème est que je ne sais pas si cela vient :
- De l'initialisation de spring
- De ma classe de test (ou tout du moins si lors de la commande mvn test le contexte est bien intialisé)
- De ma classe genericDaoImpl
Voili voilou, a vos claviers!!! Et merci d'avance de toute réponse![]()
Partager