【问题标题】:JPA Native sql resultlist castingJPA Native sql 结果列表转换
【发布时间】:2012-04-14 03:04:27
【问题描述】:

我遇到了一些问题。 我正在使用 jpa(来自 glassfish 3.1 的 eclipselink)和一个 mysql 数据库。

假设我有一个学生表,其中包含一个日期时间的“创作”字段。 如果我想选择在 '2012-03-30' 上创建的所有记录,请使用 sql,无论何时:

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

好的,它的工作。

现在我想在 jpql 中做同样的事情。而且我不认为这是可能的。 所以我尝试使用本机查询:

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);
}

我在第 2 行和第 4 行尝试了使用和不使用铸件,但结果始终相同:

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

我的学生类有 @entity 注释,使用

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

怎么了?

【问题讨论】:

  • 好的,解决了。等待 8 小时回答我的 onw 问题 ... entityManager.createNativeQuery("select * from student where date(creation) = '2012-03-30',Student.class);

标签: mysql jpa-2.0 jpql


【解决方案1】:

好的,解决了。

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

【讨论】:

    猜你喜欢
    • 2013-04-24
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多