单向多对一关联:只有在一边配置关联映射。。而另外一边不管。。
案例(一个父亲拥有多个孩子)
父亲类
public class Father implements Serializable {
private static final long serialVersionUID = -8390304441273897145L;
public Father(){
}
//父亲ID
private Integer fatherId;
//父亲姓名
private String fatherName;
public Integer getFatherId() {
return fatherId;
}
public void setFatherId(Integer fatherId) {
this.fatherId = fatherId;
}
public String getFatherName() {
return fatherName;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
}
一方: 配置关联映射
<hibernate-mapping package="net.jsp.orm"> <class name="Father" table="FATHER"> <id name="fatherId" column="FATHERID" type="java.lang.Integer"> <generator class="identity" /> </id> <property name="fatherName" column="FATHERNAME" type="java.lang.String" /> </class> </hibernate-mapping>
孩子类
public class Child implements Serializable {
private static final long serialVersionUID = 6750561728903621833L;
public Child(){
}
private Integer childId;
private String childName;
//多对一
private Father father;
public Integer getChildId() {
return childId;
}
public void setChildId(Integer childId) {
this.childId = childId;
}
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
public Father getFather() {
return father;
}
public void setFather(Father father) {
this.father = father;
}
}
多方:配置关联映射
<hibernate-mapping package="net.jsp.orm"> <class name="Child" table="CHILD"> <id name="childId" column="CHILDID"> <generator class="identity" /> </id> <property name="childName" column="CHILDNAME" /> <!-- 多方的配置 --> <many-to-one name="father" class="Father" column="FATHERID" /> </class> </hibernate-mapping>
双向多对一关联:即在父亲类也添加一个孩子对象。。那么概念即(一个孩子或许有多个父亲[养父...]) 。配置如同上面的多方配置关联。。。。
单向一对多关联:
如同上代码。。将 Child的 Father 对象去掉。 然后再Father 类中添加 set<Child> childs属性 即可。一方配置关联映射如下:
<hibernate-mapping package="net.jsp.orm"> <class name="Father" table="FATHER"> <id name="fatherId" column="FATHERID" type="java.lang.Integer"> <generator class="identity" /> </id> <property name="fatherName" column="FATHERNAME" type="java.lang.String" /> <set name="childs" > <key column="fatherId" /> <one-to-many class="Child"/> </set> </class> </hibernate-mapping>
双向一对多。就组成了。多对多的关联。。。
多对多关联 时。应当数据库有个中间关联表 比如用户表跟信息表。。一个用户可以发多条信息 ,一条信息可以响应给多个用户。。这样如果有张中间表就更直观一些。。。
<!-- 用户方的关联配置 --> <hibernate-mapping> <class name="net.jsp.orm.User" table="[USER]"> <id name="uid" column="UID"> <generator class="identity" /> </id> <property name="uname" column="UNAME" type="string" not-null="true" length="50" /> <property name="upass" type="string"> <column name="UPASS" not-null="true" length="50" /> </property> <set name="messages" table="TEMP_USER_MSG"> <key column="TEMP_USERID" /> <many-to-many class="net.jsp.orm.Message" column="TEMP_MSGID" /> </set> .............. </class> </hibernate-mapping>
<!-- 消息方的关联配置 --> <hibernate-mapping> <class name="net.jsp.orm.Message" table="[Message]"> <id name="msgId" column="MSGID"> <generator class="identity" /> </id> <property name="msgContent" column="MSGCONTENT" type="string" not-null="true" /> <set name="users" table="TEMP_USER_MSG"> <key column="TEMP_USERID" /> <many-to-many class="net.jsp.orm.User" column="TEMP_MSGID" /> </set> ........ </class> </hibernate-mapping>