hibernate一对多关联

实体之间有三种关系:

1.一对一

2.一对多

3.多对一

一对多

goods类

package entity;
/*
* 货物表
* */
public class goods {
private int gid;
private long gnum;
private inventory inv;
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public long getGnum() {
return gnum;
}
public void setGnum(long gnum) {
this.gnum = gnum;
}
public inventory getInv() {
return inv;
}
public void setInv(inventory inv) {
this.inv = inv;
}


}

配置

<?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="entity.goods" table="goods">
<id name="gid" column="gid">
<generator class="native"/>
</id>
<property name="gnum" column="gnum"></property>
<many-to-one name="inv" column="igid" class="entity.inventory"></many-to-one>

    <!--

    <many-to-one>标签

        name    :关联对象的属性的名称.

        column  :表中的外键名称.

        class   :关联对象类的全路径

    -->

</class>
</hibernate-mapping>

 

inventory类

package entity;

 

import java.util.HashSet;

import java.util.Set;

 

/*

 * 清单表

 * */

public class inventory {

private int iid;

private String iname;

//private int igid;

private Set<goods> goodses=new HashSet<goods>();

public int getIid() {

return iid;

}

public void setIid(int iid) {

this.iid = iid;

}

public String getIname() {

return iname;

}

public void setIname(String iname) {

this.iname = iname;

}

//public int getIgid() {

//return igid;

//}

//public void setIgid(int igid) {

//this.igid = igid;

//}

public Set<goods> getGoodses() {

return goodses;

}

public void setGoodses(Set<goods> goodses) {

this.goodses = goodses;

}

 

 

}

配置

<?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="entity.inventory" table="inventory">

    <id name="iid" column="iid">

    </id>

    <property name="iname" column="iname"></property>

    <!-- <property name="igid" column="igid"></property> -->

   

    <set name="goodses">

    <key column="igid"></key>

    <one-to-many class="entity.goods"/>

    </set>

    </class>

    

    </hibernate-mapping>

    <!--

    <many-to-one>标签

        name    :关联对象的属性的名称.

        column  :表中的外键名称.

        class   :关联对象类的全路径

    -->

posted @ 2016-06-29 21:42  谢坪  阅读(132)  评论(0编辑  收藏  举报