数据库
首页 > 数据库> > MySQL视图中的LONGTEXT字段的JPA本机查询导致错误

MySQL视图中的LONGTEXT字段的JPA本机查询导致错误

作者:互联网

我有以下JPA SqlResultSetMapping:

 @SqlResultSetMappings({               
    @SqlResultSetMapping(name="GroupParticipantDTO", 
            columns={ 
                @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");

视图是这样的:

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
;

GroupParticipation表的备注列定义为LONGTEXT(我使用的是Mysql 5.x)

现在出现问题:
当从查询中注释掉备注字段时,一切都运行正常,但如果我尝试在查询中包含备注字段,则会收到以下错误:

 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)

是什么赋予了?如何从本机查询中获取LONGTEXT列?

解决方法:

此问题在HHH-1483HHH-3892中报告.简而言之,Hibernate不知道如何映射本机查询返回的LONGVARCHAR列.

在Hibernate 3.5.0中修复了此问题.对于以前的版本,解决方法是扩展MysqlDialect以为LONGVARCHAR注册正确的Hibernate类型:

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,orm,jpa,hibernate,longtext
来源: https://codeday.me/bug/20190527/1160585.html