tweetysat
tweetysat

Reputation: 2403

JPA Native sql resultlist casting

I've got some problem. I'm working with jpa (eclipselink from glassfish 3.1) and a mysql db.

Let suppose I've got a student table with a 'creation' field that is a datetime. Using sql if I want to select all records created on '2012-03-30', no matter the time :

select * from student where date(created) = '2012-03-30'

Ok, its working.

Now I'd like to do the same in jpql. And I don't think it's posible. So I tried with a native query :

entityManager.createNativeQuery("select * from student where date(creation) = '2012-03-30';
List<Student> students = (List<Student>)query.getResultList();
System.out.println("Result : "+students.size());
for (Student student : (List<Student>)students)
{
    System.out.println(student);
}

I tried with and without the castings in lines 2 and 4 but always the same result :

Result : 3
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.tuto.entities.Train

My Student class has the @entity annotation and everything is working fine using

entityManager.createQuery("select s from Student s where s.age = '25');

What's wrong ?

Upvotes: 0

Views: 3319

Answers (1)

tweetysat
tweetysat

Reputation: 2403

Ok, it's resolved.

entityManager.createNativeQuery("select * from student where date(creation) = '2012-03-30',Student.class);

Upvotes: 3

Related Questions