name="number" length="20" not-null="true" />
</property>
<property name="classid" type="java.lang.Integer">
<column name="classid" not-null="true" />
</property>
</class>
</hibernate-mapping>
----------------------------------------------------------------------------------------------------------------
映射文件Score.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="score.Score" table="score" catalog="hbql">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="score" type="java.lang.Integer">
<column name="score" not-null="true" />
</property>
<property name="type" type="java.lang.String">
<column name="type" length="20" not-null="true" />
</property>
</class>
</hibernate-mapping>
---------------------------------------------------------------------------------------------------------------------
Hibernate单向一对多应注意的问题(3)
时间:2011-09-09
持久化类
public class Score implements java.io.Serializable {
private Integer id;
private Integer score;
private String type;
…..
public class Student implements java.io.Serializable {
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(10) unsigned NOT NULL auto_increment,
`name` varchar(20) NOT NULL,
`number` varchar(20) NOT NULL,
`classid` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `hbql`.`score`;
CREATE TABLE `hbql`.`score` (
`id` int(10) unsigned NOT NULL auto_increment,
`score` int(10) unsigned NOT NULL,
`type` varchar(20) NOT NULL,
`sid` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_score_1` (`sid`),
CONSTRAINT `FK_score_1` FOREIGN KEY (`sid`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
能解决此问题主要得益于该BLOG:http://suhaoyuan.spa |