MySQL数据库中的类型为longBlob
实体类定义为:byte[] 类型
映射文件配置为:type="org.springframework.orm.hibernate3.support.BlobByteArrayType
异常信息:
1 org.hibernate.MappingException: No Dialect mapping for JDBC type: -4 2 at org.hibernate.dialect.TypeNames.get(TypeNames.java:56) 3 at org.hibernate.dialect.TypeNames.get(TypeNames.java:81) 4 at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:370) 5 at org.hibernate.loader.custom.CustomLoader$Metadata.getHibernateType(CustomLoader.java:559)
解决办法:
1、添加注册类:
1 package com.prochanges.framework.utils; 2 3 import java.sql.Types; 4 5 import org.hibernate.Hibernate; 6 import org.hibernate.dialect.MySQL5Dialect; 7 8 public class BlobMySQLDialect extends MySQL5Dialect { 9 public BlobMySQLDialect() { 10 super(); 11 registerHibernateType(Types.LONGVARBINARY, Hibernate.BLOB.getName());// 注册blob,Types.LONGVARBINARY的值就是-4 12 } 13 }
2、修改配置文件中数据库的hibernate.dialec属性的值,即为刚写的注册类:
<prop key="hibernate.dialect">com.prochanges.framework.utils.BlobMySQLDialect</prop>
No Dialect mapping for JDBC type–4 hibernate查询MySQL中longBlob数据