jpa查询blob数据抛出异常:No Dialect mapping for JDBC type -4

 用entityManager.createNativeQuery(sql).list()查询blob数据时,抛出了下面的异常:

Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: -4
    at org.hibernate.dialect.TypeNames.get(TypeNames.java:56)
    at org.hibernate.dialect.TypeNames.get(TypeNames.java:81)
    at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:370)

其中entityManager为javax.persistence.EntityManager类型,此时,需要注册BLOB类型,编写一个继承MySQL5Dialect的类:

package com.kin.mail.dialect; import java.sql.Types; import org.hibernate.Hibernate; import org.hibernate.dialect.MySQL5Dialect; /** * 注册BLOB类型 * @author kin * */ public class BlobMySQLDialect extends MySQL5Dialect { public BlobMySQLDialect () { super(); registerHibernateType(Types.LONGVARBINARY, Hibernate.BLOB.getName());//注册blob,Types.LONGVARBINARY的值就是-4 } }

然后在persistence.xml配置文件里修改数据库方言声明:

<property name="hibernate.dialect" value="com.kin.mail.dialect.BlobMySQLDialect" />

你可能感兴趣的:(sql,mysql,数据库,jdbc,jpa,Class)