Hibernate整合MySql报错信息

1.hibernate插入错误。

ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group, identity, salt, status) values ('201417010128', 0, 0, '0c67c5eb515aa6e958' at line 1

原因:group是关键字。。


2.ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Field 'mobile' doesn't have a default value

Field 'mobile' doesn't have a default value

原因:这个问题我在公司前两天也遇到过,向表中新增字段后,由于设置了not null,在插入的时候该字段如果为null就会报错。可以将该字段在数据库设置一个默认值。在mysql5.0.2以前,mysql对非法值检查并不严厉,而且为了数据输入还会强制将他们变为合法值。在5.0.2以后的版本中,保留了以前的默认行为,但你可以为不良值选择更传统的处理方法,从而使得服务器能够拒绝并放弃出现不良值的语句。


3.org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value :

org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value :

原因:在.hbm.xml文件设置的not-null不是mysql里面的not-null,在插入的时候会报错。


image.png


4.java.lang.ClassCastException: org.springframework.orm.hibernate5.SessionHolder cannot be cast to org.springframework.orm.hibernate4.SessionHolder

java.lang.ClassCastException: org.springframework.orm.hibernate5.SessionHolder cannot be cast to org.springframework.orm.hibernate4.SessionHolder

原因:原因很明显,所用的hibernate版本为5.0.12,Spring版本为4.3.14,在用HibernateDaoSupport时引用了hibernate4对应的版本。在Spring提供的orm包中,存在hibernate3、4、5版本。修改为hibernate5的support。

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

image.png

-------------------------------2018-04-20 18:54:54-----------------------------------

5.org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed

描述:数据库没有做外键关联,hibernate实体类里面有对象关系

解决方法:1.在配置这个类的配置文件中many-to-one标签中添加fetch=“join” 这个还会报异常,不过可以查询出来了

     2.在配置这个类的配置文件中many-to-one标签中添加lazy=“false”,不延迟加载,就没有问题了


6.should be mapped with insert="false" update="false"

描述:在实体类中配置实体关系的时候,我既想保留属性列,又想增加实体关系。出现了这个错误

解决办法:在标签内增加 insert="false" update="false"

-------------------------------2018-04-22 00:03:05-----------------------------------

7.org.hibernate.HibernateException: createQuery is not valid without active transaction

描述:是dao中使用sessionFactory.getCurrentSession()方法获得session,在配置文件中写了这个

<prop key="hibernate.current_session_context_class">thread</prop>

解决办法:删掉上面这个配置,参考


8.at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663) ~[jackson-databind-2.5.0.jar:2.5.0]

描述:jackson 序列化Hibernate entity类无限递归的问题。

解决方法:使用@JsonIgnore,@JsonIdentityInfo,@JsonManagedReference and @JsonBackReference这几个注解,我直接放弃了那个表的实体关联,手动查询。



:D 一言句子获取中...