英文:
Cast exception when trying to get the resultset in hibernate "Cannot cast 'java.lang.Object[]' to"
问题
在我的应用程序中,我使用Hibernate 3.3进行数据连接。当我尝试通过本地查询获取结果集时,它会抛出Cannot cast 'java.lang.Object[]' to org.model.Term
异常。
模型类:
@Entity
@Table(name = "term")
public class Term {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "version")
private long versionId;
@Column(name = "name")
private String name;
@Column(name = "created_date")
private Date createdDate;
}
数据访问对象方法:
@Repository
@Transactional
public class TermsDao {
private static final String findLatestQuery = "select * from term WHERE version = (select max(version) from term)";
@PersistenceContext
private EntityManager entityManager;
@Transactional(readOnly = true)
public Term findLatestTerm() {
Query query = entityManager.createNativeQuery(findLatestQuery);
return (Term) query.getSingleResult();
}
}
持久化配置:
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.model.Term</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<!--<property name="hibernate.hbm2ddl.auto" value="update"/>-->
</properties>
</persistence-unit>
</persistence>
表结构:
Field Type Key Extra
version bigint(20) PRI auto_increment
name varchar(255)
created_date datetime
当评估query.getSingleResult();
时,结果是存在的。
query.getSingleResult().getClass()
返回[Ljava.lang.Object;
。
可能是什么原因导致了这个问题?
英文:
In my application im using Hibernate 3.3 for the data connection. When im trying to get the resultset by a native query, it throws Cannot cast 'java.lang.Object[]' to org.model.Term
exception.
Model class :
@Entity
@Table(name = "term")
public class Term {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "version")
private long versionId;
@Column(name = "name")
private String name;
@Column(name = "created_date")
private Date createdDate;
}
Dao method :
@Repository
@Transactional
public class TermsDao {
private static final String findLatestQuery = "select * from term WHERE version = (select max(version) from term)";
@PersistenceContext
private EntityManager entityManager;
@Transactional(readOnly = true)
public Term findLatestTerm() {
Query query = entityManager.createNativeQuery(findLatestQuery);
return (Term) query.getSingleResult();
}
}
persistence config :
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.model.Term</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<!--<property name="hibernate.hbm2ddl.auto" value="update"/>-->
</properties>
</persistence-unit>
</persistence>
table structure :
Field Type Key Extra
version bigint(20) PRI auto_increment
name varchar(255)
created_date datetime
When query.getSingleResult();
is evaluated the results are there.
query.getSingleResult().getClass() returns `[Ljava.lang.Object;`
What might be causing this issue?
答案1
得分: 3
EntityManager.createNativeQuery(String sqlString) 用于 UPDATE 和 DELETE 语句:
> 如果查询不是更新或删除查询,则查询执行将导致将 SQL 结果的每一行作为 Object[] 类型的结果返回(如果选择列表中只有一列,则返回类型为 Object)。
请改用 createNativeQuery(String sqlString, Class resultClass),例如:
Query query = entityManager.createNativeQuery(findLatestQuery, Term.class);
英文:
EntityManager.createNativeQuery(String sqlString) is meant for UPDATE and DELETE statements:
> If the query is not an update or delete query, query execution will result in each row of the SQL result being returned as a result of type Object[] (or a result of type Object if there is only one column in the select list.)
Use createNativeQuery(String sqlString, Class resultClass) instead, i.e:
Query query = entityManager.createNativeQuery(findLatestQuery, Term.class);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论