• hibernate error 及 sql


    1.No validator could be found for constraint

    validating type 'java.math.BigInteger'. Check configuration for 'id'
    constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:229)
    ...
    engine.ValidatorImpl.validate(ValidatorImpl.java:207)
    解决办法:
    @NotEmpty 用在集合类上面;不能为null,而且长度必须大于0,(" “,” ")
    @NotBlank 只能作用在String上面, 不能为null,而且调用trim()后,长度必须大于0 (“test”) 即:必须有实际字符
    @NotNull 用在基本类型,整型上(Integer、Long、Short…),对象也可以用这个,但是对象内部里面就不能校验了,不能为null,但可以为empty,(""," “,” ")
    

    2.org.hibernate.exception.ConstraintViolationException: could not execute statement

    解决办法:
    违反约束异常,无法执行语句.
    很有可能是某个字段数据库不能为null,而传过去的值为null
    仔细检查约束条件
    

    3.实体注解 No identifier specified for entity

    org.hibernate.AnnotationException: No identifier specified for entity
    解决办法:
    没有添加加主键的注解@Id,这个是必须的。但是我的实体类中明明已经添加了@Id
    

    4.保存 IdentifierGenerationException

    org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()
    解决办法:
    错误提示是说,id的生成错误,在保存之前,需先生成,增加注解,id生成策略,自增
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    

    5.sql语法判断条件参数类型不一致 FreeMarker template error Can't compare values of these types.

    FreeMarker template error (DEBUG mode; use RETHROW in production!):
    Can't compare values of these types. Allowed comparisons are between two numbers, two strings, two dates, or two booleans.
    Left hand operand is a number (wrapper: f.t.SimpleNumber).
    Right hand operand is a string (wrapper: f.t.SimpleScalar).
    The blamed expression:
    ==> status == "-1"  [in template "queryxxxCount" at line 35, column 58]
    解决办法:
    error:左右两边不能是不一样的类型, status是 int 值,sql模板中正确应该是status == -1
    

    6.hibernate 遍历集合, hql语法基于freemarker的语法,扩展了include_hql 标签

    <sql-query id="queryUserByids">
      <![CDATA[
          select * from user where id in
          (
            <#list ids as id>
                <#if ids?size == id_index +1>
                    '${id}'
                <#else>
                    '${id}',
                </#if>
            </list>
          )
      ]]>
    </sql-query>
    

    7.强制转换map错误 Caused by: java.lang.ClassCastException

    Caused by: java.lang.ClassCastException: com.xxx.xxxEntity cannot be cast to java.util.Map
    	at org.hibernate.property.access.internal.PropertyAccessMapImpl$SetterImpl.set(PropertyAccessMapImpl.java:102)
    	at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:78)
    	at org.hibernate.hql.internal.HolderInstantiator.instantiate(HolderInstantiator.java:75)
    	at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:435)
    	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429)
    	at org.hibernate.loader.Loader.list(Loader.java:2424)
    解决办法:
    修改具体sql,检查sql的返回查询列的名字是否和定义的实体对象的属性名称是否一致
    源码跟踪:从sql中获取result,然后放入list中,放入的时候需要反射list对象的属性值,tuple[i] 是具体内容值,result是对象class
             使用反射,获取对应sql 属性名称 ,通过反射设置对象属性值    
    
  • 相关阅读:
    AsyncTask Note
    从今以后我要写我写过的技术及代码
    金额阿拉伯数转中文
    [导入]匿名方法引起的异常
    [导入]对.net事件的看法
    [导入]把DataGrid控件定制成漂亮的课程表
    [导入]IIs上的证书安装要点
    oracle创建表空间
    如何在命令行更改IP地址
    server.mappath 与page命名空间说明
  • 原文地址:https://www.cnblogs.com/yaozhixiang/p/15074606.html
Copyright © 2020-2023  润新知