这个 问题困扰我多天终于干掉了
本人使用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=?