這個 問題困擾我多天終於干掉了
本人使用myeclipsega進行hibernate一對多單向關聯實例
一直報如下異常
Hibernate:
insert
into
hbqlscore
(score type)
values
:: DEBUG JDBCExceptionReporter: could not insert: [scoreScore] [insert into hbqlscore (score type) values (? ?)]
javasqlSQLException: Field sid doesnt have a default value
at commysqljdbcSQLErrorcreateSQLException(SQLErrorjava:)
//太長發不了省略點
:: WARN JDBCExceptionReporter: SQL Error: SQLState: HY
:: ERROR JDBCExceptionReporter: Field sid doesnt have a default value
保存對象studentStudent失敗!
:: DEBUG JDBCTransaction: rollback
orghibernateexceptionGenericJDBCException: could not insert: [scoreScore]
at orghibernateexceptionSQLStateConverterhandledNonSpecificException(SQLStateConverterjava:)
at orghibernateexceptionSnvert(SQLStateConverterjava:)
//太長發不了省略點
Caused by: javasqlSQLException: Field sid doesnt have a default value
at commysqljdbcSQLErrorcreateSQLException(SQLErrorjava:)
測試程序如下
Student student=new Student(huizhi);
Score score=new Score();
Score score=new Score();
Set set=new HashSet();
setadd(score);
setadd(score);
studentsetScores(set);
saveObject(student);
printStudents();
映射文件Studenthbmxml
<?xml version= encoding=utf?>
<!DOCTYPE hibernatemapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN
hiber/hibernatemappingdtd>
<hibernatemapping>
<class name=studentStudent table=student catalog=hbql >
<id name=id type=javalangInteger>
<column name=id />
<generator class=native />
</id>
<set name=scores table=score cascade=saveupdate inverse=false>
<key>
<column name=sid notnull=true />
</key>
<onetomany class=scoreScore />
</set>
<property name=name type=javalangString>
<column name=name length= notnull=true />
</property>
<property name=number type=javalangString>
<column name=number length= notnull=true />
</property>
<property name=classid type=javalangInteger>
<column name=classid notnull=true />
</property>
</class>
</hibernatemapping>
映射文件Scorehbmxml
<?xml version= encoding=utf?>
<!DOCTYPE hibernatemapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN
hiber/hibernatemappingdtd>
<hibernatemapping>
<class name=scoreScore table=score catalog=hbql>
<id name=id type=javalangInteger>
<column name=id />
<generator class=native />
</id>
<property name=score type=javalangInteger>
<column name=score notnull=true />
</property>
<property name=type type=javalangString>
<column name=type length= notnull=true />
</property>
</class>
</hibernatemapping>
持久化類
public class Score implements javaioSerializable {
private Integer id;
private Integer score;
private String type;
…
public class Student implements javaioSerializable {
private Integer id;
private String name;
private String number;
private Integer classid;
private Set scores=new HashSet();
…
數據庫表MySQL
DROP TABLE IF EXISTS `hbql``student`;
CREATE TABLE `hbql``student` (
`id` int() unsigned NOT NULL auto_increment
`name` varchar() NOT NULL
`number` varchar() NOT NULL
`classid` int() unsigned NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf;
DROP TABLE IF EXISTS `hbql``score`;
CREATE TABLE `hbql``score` (
`id` int() unsigned NOT NULL auto_increment
`score` int() unsigned NOT NULL
`type` varchar() NOT NULL
`sid` int() unsigned NOT NULL
PRIMARY KEY (`id`)
KEY `FK_score_` (`sid`)
CONSTRAINT `FK_score_` FOREIGN KEY (`sid`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf;
留意<set>元素中的inverse屬性該屬性表示關聯關系由主控(一方)還是受控方(多方)維護所謂關聯關系的維護就是受控方的外鍵插入由誰來控制inverse默認為false表示由主控方來控制當主控方控制時插入SQL的語句會分兩條進行
insert into Items(itemName itemPrice orderId) values(aa NULL);
update Items set orderId = where itemName = aa and itemPrice = ;
因為主控方控制關聯關系意味受控方在插入數據時不會考慮其外鍵引用直接插入為NULL直到主控方執行更新操作
因為HIbernate分兩條SQL語句插入Score對象所以在SCORE表中第一次外鍵為空所以定義數據庫中外鍵時默認值應為NULL 第二次是更新該條記錄的外鍵本人在創建SCORE表外鍵SID默認是不能為空所以會出現此種情況更改為NULL一切正常
如下為HIbernate生成的SQL語句
部分
Hibernate:
insert
into
hbqlscore
(score type)
values
(? ?)
Hibernate:
update
hbqlscore
set
sid=?
where
id=?
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28575.html