Hibernate 命名查询 ClassCastException
Hibernate named query ClassCastException
这可能与 重复,但没有得到正确的解决方案。
我正在使用命名查询来获取一些细节,如下所示,并作为 string、
的列表返回
<sql-query name="getContactIds">
<return-scalar column="id" type="string" />
<![CDATA[Select c.id as id from CRM.dbo.contact c where is_deleted=0]]>
</sql-query>
但是在调用此查询时,我得到 java.lang.RuntimeException: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
SearchTemplate searchTemplate = new SearchTemplate();
searchTemplate.setNamedQuery("getContactIds");
searchTemplate.setNamedQueryResultType(String.class);
salesforceContactIds=contactDao.getSfContactIds(searchTemplate);
感谢这方面的任何帮助。
JPQL 是来自 Hibernate/JPA 的查询语言。它适用于实体而不是 tables。因此,如果您的 Contact java 实体的字段 deleted 映射到 Contact table
@Entity
@Table(name = "CONTACT", schema = "name")
public class Contact {
@Id
private Integer id;
@Column(name = "is_deleted", nullable = false)
private boolean deleted;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
那么 select 应该是这样的
select * from Contact c where c.deleted = 0
然后从Contact.
获取id或其他必要字段
如果你想使用 SQL 语法,那么你需要使用 NamedNativeQuery
这可能与
<sql-query name="getContactIds">
<return-scalar column="id" type="string" />
<![CDATA[Select c.id as id from CRM.dbo.contact c where is_deleted=0]]>
</sql-query>
但是在调用此查询时,我得到 java.lang.RuntimeException: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
SearchTemplate searchTemplate = new SearchTemplate();
searchTemplate.setNamedQuery("getContactIds");
searchTemplate.setNamedQueryResultType(String.class);
salesforceContactIds=contactDao.getSfContactIds(searchTemplate);
感谢这方面的任何帮助。
JPQL 是来自 Hibernate/JPA 的查询语言。它适用于实体而不是 tables。因此,如果您的 Contact java 实体的字段 deleted 映射到 Contact table
@Entity
@Table(name = "CONTACT", schema = "name")
public class Contact {
@Id
private Integer id;
@Column(name = "is_deleted", nullable = false)
private boolean deleted;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
那么 select 应该是这样的
select * from Contact c where c.deleted = 0
然后从Contact.
获取id或其他必要字段如果你想使用 SQL 语法,那么你需要使用 NamedNativeQuery