MySQL视图中的LONGTEXT字段的JPA本地查询导致错误 [英] JPA native query for LONGTEXT field in a MySQL view results in error
问题描述
我有以下JPA SqlResultSetMapping:
@SqlResultSetMappings({
@SqlResultSetMapping(name =GroupParticipantDTO ,
列= {
@ColumnResult(name =gpId),
@ColumnResult(name =gpRole),
// @ColumnResult(name =gpRemarks )
}
)
这样使用:
StringBuilder sbQuery = new StringBuilder(Select);
sbQuery.append(gpId,);
sbQuery .append(gpRole,);
// sbQuery.append(gpRemarks);
sbQuery.append(FROM v_group_participants_with_details);
Query query = em.createNativeQuery(sbQuery.toString(),GroupParticipantDTO);
像这样:
$ b $ pre $ code $ DROP VIEW如果存在`v_group_participants_with_details`;
CREATE VIEW`v_group_participants_wit h_details`
AS
SELECT
gp.id AS gpId,
gp.role AS gpRole,
gp.remarks AS gpRemarks
FROM GroupParticipation gp
;
GroupParticipation表的备注列定义为LONGTEXT(我正在使用Mysql 5.x)
现在解决问题:
当查询字段从查询中被注释掉时,所有的工作都完美无缺,但是如果我尝试在查询中包含备注字段,我得到以下错误:
javax.persistence.PersistenceException:org.hibernate.MappingException:
无方言映射JDBC类型:-1
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException
(AbstractEntityManagerImpl.java:614)
位于org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76 )
什么给了?如何从本地查询获取LONGTEXT列?
这个问题在 HHH-1483 和 HHH-3892 。简而言之,Hibernate不知道如何映射本地查询返回的 LONGVARCHAR
列。
这个问题在Hibernate 3.5.0+中修复。对于以前的版本,解决方法是扩展 MysqlDialect
,为<$ c $注册正确的Hibernate 类型
c> LONGVARCHAR :
import java.sql.Types;
导入org.hibernate.Hibernate;
public class MyMySQL5Dialect extends org.hibernate.dialect.MySQL5Dialect {
public MyMySQL5Dialect(){
super();
//注册额外的hibernate类型,默认用于标量sqlquery类型自动检测
registerHibernateType(Types.LONGVARCHAR,Hibernate.TEXT.getName());
}
}
I have the following JPA SqlResultSetMapping:
@SqlResultSetMappings({
@SqlResultSetMapping(name="GroupParticipantDTO",
columns={
@ColumnResult(name="gpId"),
@ColumnResult(name="gpRole"),
// @ColumnResult(name="gpRemarks")
}
)
Which is used like this:
StringBuilder sbQuery = new StringBuilder("Select ");
sbQuery.append(" gpId, ");
sbQuery.append(" gpRole, ");
// sbQuery.append(" gpRemarks ");
sbQuery.append(" FROM v_group_participants_with_details ");
Query query = em.createNativeQuery(sbQuery.toString(), "GroupParticipantDTO");
The view is like this:
DROP VIEW IF EXISTS `v_group_participants_with_details`;
CREATE VIEW `v_group_participants_with_details`
AS
SELECT
gp.id AS gpId,
gp.role AS gpRole,
gp.remarks AS gpRemarks
FROM GroupParticipation gp
;
The GroupParticipation table has the remarks column defined as LONGTEXT (I'm using Mysql 5.x)
Now for the problem: When the remarks field is commented out from the query everything works perfectly, but if I try to include the remarks field in the query, I get the following error:
javax.persistence.PersistenceException: org.hibernate.MappingException:
No Dialect mapping for JDBC type: -1
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException
(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76)
What gives? How can I get a LONGTEXT column from a native query?
This problem is reported in HHH-1483 and HHH-3892. In short, Hibernate does not know, how to map a LONGVARCHAR
column returned by a native query.
This problem is fixed in Hibernate 3.5.0+. For previous versions, a workaround would be to extend the MysqlDialect
to register the correct Hibernate Type
for a LONGVARCHAR
:
import java.sql.Types;
import org.hibernate.Hibernate;
public class MyMySQL5Dialect extends org.hibernate.dialect.MySQL5Dialect {
public MyMySQL5Dialect() {
super();
// register additional hibernate types for default use in scalar sqlquery type auto detection
registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName());
}
}
这篇关于MySQL视图中的LONGTEXT字段的JPA本地查询导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!