環(huán)境:
? mysql:5.7.19
? Hibernate:5.2.3
Error1
異常:Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernate.hibernate_sequence' doesn't exist
解決:配置方言為<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
原因:hibernate自動建表出錯(cuò),但是手動在MySql數(shù)據(jù)庫中建表成功后卻能用hibernate對數(shù)據(jù)庫內(nèi)容進(jìn)行CRUD操作,很可能原因?yàn)閐ialect方言配置不當(dāng)最铁。在mysql 5.0版本以后不支持 type=InnoDB 關(guān)鍵字躁倒,需要使用 engine=InnoDB炮叶,配置方言為:
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
更改完畢后建表成功
Error2
異常:org.hibernate.MappingException: Could not determine type for:
原因及解決:Hibernate 注解 @OneToOne 時(shí)候,出現(xiàn)以下錯(cuò)誤,經(jīng)調(diào)試,發(fā)現(xiàn)在同一個(gè)類中相味,注解要么全部放在字段上,要么全部放在get方法上储矩,不能混合使用锌云!
Error3
異常:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`th`.`cst_linkman`, CONSTRAINT `FKh9yp1nql5227xxcopuxqx2e7q` FOREIGN KEY (`lkm_cust_id`) REFERENCES `cst_customer` (`cust_id`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
原因:使用一對多注解來解決外鍵問題時(shí)荠医,多方實(shí)體類添加了外鍵字段
解決:將多方實(shí)體類外鍵相關(guān)字段及get,set方法刪除在進(jìn)行執(zhí)行問題解決
備注:數(shù)據(jù)庫對應(yīng)表結(jié)構(gòu)生成時(shí),均不需要對外鍵進(jìn)行字段描述桑涎,即均不需要添加外鍵字段彬向,外鍵字段均有注解進(jìn)行描述
Error4
異常:Hibernate:5.2.11版本根據(jù)數(shù)據(jù)庫實(shí)體類無法生成外鍵
解決:Hibernate版本改為5.2.3版本即可
原因:未知