Hibernate单向一对多应注意的问题
时间:2011-09-09
这个 问题困扰我10多天,终于干掉了。
本人使用myeclipse6.5ga,进行hibernate一对多单向关联实例。
一直报如下异常:
Hibernate:
insert
into
hbql.score
(score, type)
values
。。。。。。。。。。。。。。。。。。。
17:03:32,484 DEBUG JDBCExceptionReporter:69 - could not insert: [score.Score] [insert into hbql.score (score, type) values (?, ?)]
java.sql.SQLException: Field ''sid'' doesn''t have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
..................//太长发不了。省略点
17:03:32,484 WARN JDBCExceptionReporter:77 - SQL Error: 1364, SQLState: HY000
17:03:32,484 ERROR JDBCExceptionReporter:78 - Field ''sid'' doesn''t have a default value
保存对象student.Student失败!
17:03:32,484 DEBUG JDBCTransaction:152 - rollback
org.hibernate.exception.GenericJDBCException: could not insert: [score.Score]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
...........................//太长发不了。省略点
Caused by: java.sql.SQLException: Field ''sid'' doesn''t have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
测试程序如下:
Student student=new Student("huizhi","2708",200);
Score score1=new Score(98,"1");
Score score2=new Score(90,"2");
Set set=new HashSet();
set.add(score1);
set.add(score2);
student.setScores(set);
saveObject(student);
printStudents();
----------------------------------------------------------------------------------------------------------------------
Hibernate单向一对多应注意的问题(2)
时间:2011-09-09
映射文件Student.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="student.Student" table="student" catalog="hbql" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<set name="scores" table="score" cascade="save-update" inverse="false">
<key>
<column name="sid" not-null="true" />
</key>
<one-to-many class="score.Score" />
</set>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true" />
</property>
<property name="number" type="java.lang.String">
<column
|