hibernate mysql 视图查询,MySQL视图中的LONGTEXT字段的JPA本地查询导致错误

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值