hibernate model mapping exception

来源:互联网 发布:mysql和redis 的结合 编辑:程序博客网 时间:2024/05/16 16:01

在使用hibernate将查询出的数据映射到实体类时,出现映射错误,原因是有些特殊类型的字段不能直接进行映射,比如image等

解决方法,spring配置文件中,"sessionFactory"bean的有一项hibernate.dialect,是指定hibernate所使用的方言的,他的值可以写到一个自定义的property文件中,内容如下

#sessionFactory
#hibernate.dialect = org.hibernate.dialect.SQLServerDialect
hibernate.dialect = cpcns.gongyongzujian.util.MySQLServerDialect
#hibernate.show_sql = false;
hibernate.show_sql = true;

 

<bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
  
  <property name="mappingJarLocations">
   <list>
    <value>WEB-INF/lib/cpcns_base.jar</value>
    <value>WEB-INF/lib/CPCNS_ZuJianBase.jar</value>
           </list>
  </property>
  
  <property name="mappingDirectoryLocations">
   <list>
                <value>classpath:/cpcns/gongyongzujian/wenjianchuanshu/model</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.connection.autocommit">true</prop> 
    <prop key="hibernate.connection.release_mode">after_statement</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.jdbc.batch_size">10</prop>
   </props>
  </property>
 </bean>

 

我们可以继承它的SQLServerDialect类,在自己定义的类里做下转换

 

public class MySQLServerDialect extends SQLServerDialect{
 public MySQLServerDialect() {
     super();
     registerHibernateType(Types.LONGVARBINARY, Hibernate.BLOB.getName());
 } 

}

 


同时将hibernate.dialect 的值改为 cpcns.gongyongzujian.util.MySQLServerDialect,及在.property中如下设置


 hibernate.dialect = cpcns.gongyongzujian.util.MySQLServerDialect

原创粉丝点击