1.No validator could be found for constraint
No validator could be found for constraint 'org.hibernate.validator.constraints.NotBlank' validating type 'java.math.BigInteger'. Check configuration for 'id'
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:229)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:310)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(ConstraintTree.java:244)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:163)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:116)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:73)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:620)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:583)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForNonDefaultGroup(ValidatorImpl.java:543)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:457)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:410)
at org.hibernate.validator.internal.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 属性名称 ,通过反射设置对象属性值