Problème de cast avec @NamedQuery
Salut :)
j'ai créé deux classes: victimeDAOImpl et victimeDAOInterf
je fais le test avec JUnit de la methode : getVictimeById et ça retourne une erreur
ci dessous les données suivantes:
_les deux classes : VictimeDaoImpl.java et VictimeDaoInterf.java
_la classe : Victime.java
_la classe de test VictimeDaoTest.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
@Entity
@NamedQuery(name = "Victime.ById", query = "from Victime where victimeId = :victimeId")
@Table(name="victime" ,schema="testdb")
public class Victime implements Serializable {
@Id
@GeneratedValue
@Column(name="victime_id")
private int victimeId;
// code omitted |
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
public interface VictimeDAOInterf {
public Victime getVictimeById(int victimeId) throws DataAccessException;
public Victime getVictimeByMatricule( String victimeMatricule) throws DataAccessException;
public List<Victime> getAllVictimes() throws DataAccessException;
public void saveVictime(Victime v) throws DataAccessException;
public void removeVictime(Victime v) throws DataAccessException;
public void updateVictime(Victime v) throws DataAccessException;
} |
Code:
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
|
@Transactional
@Repository("victimeDao")
public class VictimeDAOImpl implements VictimeDAOInterf {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public Victime getVictimeById(int victimeId) {
return (Victime) sessionFactory.getCurrentSession().getNamedQuery("Victime.ById").setParameter("victimeId", victimeId);
//return (Victime) sessionFactory.getCurrentSession().get(Victime.class, victimeId);
}
// code omitted |
et la classe de JUnit :
Code:
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
|
public class VictimeDAOTest {
private static ApplicationContext ctx = null;
private static Victime record = null;
private static VictimeDAOInterf dao = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
dao = (VictimeDAOInterf) ctx.getBean("victimeDao");
}
@After
public void tearDown() throws Exception {
dao = null;
}
@Test
public void testGetVictimeById() {
Victime record = dao.getVictimeById(1);
assertNotNull(record);
assertEquals(1,record.getVictimeId());
System.out.println(record+"\n");
}
//code omitted |
voici l'erreur de JUnit:
Code:
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
|
java.lang.ClassCastException: org.hibernate.impl.QueryImpl cannot be cast to org.ocpgroup.domain.model.Victime
at org.ocpgroup.dao.impls.VictimeDAOImpl.getVictimeById(VictimeDAOImpl.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy30.getVictimeById(Unknown Source)
at org.ocpgroup.dao.impls.VictimeDAOTest.testGetVictimeById(VictimeDAOTest.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) |
S'il vous plait j'ai besoin de votre aide ;)