hibernate一对多
来源:互联网 发布:淘宝乐高回力车 编辑:程序博客网 时间:2024/06/06 01:56
今天把ssh中hibernate一对多双向实现了下,贴出来分享下,有不足的,请兄弟们指正.
最主要的两个model person(1端) score(多端),一个人可能有多个分数.
package com.fk.model;
import java.util.HashSet;
import java.util.Set;
public class Person {
private int id;
private String name;
private String phone;
private String address;
private Set<Score> scores = new HashSet<Score>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Set<Score> getScores() {
return scores;
}
public void setScores(Set<Score> scores) {
this.scores = scores;
}
}
package com.fk.model;
public class Score {
private int id;
private String scorename;
private int scorevalue;
private Person person;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getScorename() {
return scorename;
}
public void setScorename(String scorename) {
this.scorename = scorename;
}
public int getScorevalue() {
return scorevalue;
}
public void setScorevalue(int scorevalue) {
this.scorevalue = scorevalue;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
两个实体类,关键是 在person中要设置`set集合,集合的类型是Set<Score>,在Score中要设置Person.
首先来讲,为啥要设置双向关联阿,其实单项也ok,不过单项关联,在存储的时候,会发出多条update语句,单项的话,就没有必要在score中设置person了,单项存储的话,流程是这样,先存储score,再创建person对象,然后把set集合设置上.这样会发update语句,更新score表中的外键字段.
维护关系等与在一的一端维护,要存1个person,100条score,发1个insert person,100条insertscore,外加100条update,
如果就发100条insert score 1条person多好,这样就必须用到双向关联了.
person.hbm.xml
<hibernate-mapping>
<class name="com.fk.model.Person" table="t_person">
<id name="id" column="id" type="int">
<generator class="increment" />
</id>
<property name="name" />
<property name="phone" />
<property name="address" />
<set name="scores" inverse="true">
<key column="person_id"></key>
<one-to-many class="com.fk.model.Score"></one-to-many>
</set>
</class>
</hibernate-mapping>
这个注意inverse,说明这个维护关系转到score来搞,不要person搞了,不要发update了
<hibernate-mapping>
<class name="com.fk.model.Score" table="t_score">
<id name="id" column="id" type="int">
<generator class="increment" />
</id>
<property name="scorename" column="scorename" type="string" />
<property name="scorevalue" column="scorevalue" type="int" />
<many-to-one name="person" column="person_id"></many-to-one>
</class>
</hibernate-mapping>
添加了多对一的标签了,name属性填Score中person属性,column中填person_id,外键.
保存时候,先存person,不需要知道person的set集合了,再存score,在存score时,把person设置上,就可以了.
- Hibernate 一对多例
- hibernate一对多例子
- hibernate 一对多映射
- hibernate一对多
- hibernate一对多 更新
- Hibernate一对多映射
- Hibernate 一对多
- Hibernate 一对多映射
- Hibernate一对多(单向)
- Hibernate一对多(双向)
- hibernate一对多
- Hibernate 一对多映射
- hibernate 一对多映射
- hibernate一对多实例
- Hibernate一对多实例
- hibernate一对多
- hibernate 一对多删除
- Hibernate 一对多问题
- iOS表分区头和表视图的编辑模式
- Jquery according
- C++标准库中队列的应用
- JAVA正则表达式语法大全
- mysql无法启动ERROR! MySQL is running but PID file could not be found ?
- hibernate一对多
- 《统计学习方法》学习笔记(三)——K近邻法
- NJUPT 微机 中断系统 日时钟中断之替换1CH实现字符串动态显示
- 如果你是IT技术人员,请思考这15个问题
- 分享自己的一些android util 源文件
- 1085. Perfect Sequence
- iOS9下App Store新应用提审攻略
- Android 蓝牙BLE (蓝牙成长之路)1
- 千万级到10亿+的疯涨,搜狗商业平台服务化体系实践之路