Cast exception when trying to get the resultset in hibernate "Cannot cast 'java.lang.Object[]' to"

huangapple go评论87阅读模式
英文:

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 &#39;java.lang.Object[]&#39; to org.model.Term exception.

Model class :

@Entity
@Table(name = &quot;term&quot;)
public class Term {

   @Id
   @GeneratedValue(strategy= GenerationType.AUTO)
   @Column(name = &quot;version&quot;)
   private long versionId;

   @Column(name = &quot;name&quot;)
   private String name;

   @Column(name = &quot;created_date&quot;)
   private Date createdDate;
}

Dao method :

  @Repository
  @Transactional
  public class TermsDao {

  private static final String findLatestQuery = &quot;select * from term WHERE version = (select max(version) from term)&quot;;

    @PersistenceContext
    private EntityManager entityManager;

    @Transactional(readOnly = true)
    public Term findLatestTerm() {

    Query query = entityManager.createNativeQuery(findLatestQuery);
    return (Term) query.getSingleResult();
   }

}

persistence config :

&lt;persistence version=&quot;1.0&quot;
         xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence
     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
&lt;persistence-unit name=&quot;persistenceUnit&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt;
    &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
    &lt;class&gt;org.model.Term&lt;/class&gt;
    &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
    &lt;properties&gt;
        &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.MySQL5InnoDBDialect&quot;/&gt;
        &lt;!--&lt;property name=&quot;hibernate.hbm2ddl.auto&quot; value=&quot;update&quot;/&gt;--&gt;
    &lt;/properties&gt;
&lt;/persistence-unit&gt;

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

huangapple
  • 本文由 发表于 2020年9月27日 20:21:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64088410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定