Hibernate自关联关系

2008-02-23 09:17:21来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

Hibernate自关联关系
业务逻辑:
书籍的种类,本身是自关联的关系,如下图所示:
所有书籍:
历史书籍
音乐书籍
钢琴书籍
烹饪书籍
美食书籍

1. Books类的源程序
Books.Java
package mypack;
import java.util.Set;
import java.io.Serializable;
public class Books
implements Serializable {
/**
* 默认构造函数
*/
public Books() {
}
/** 主健id */
private Long id;
/** 书籍名称 */
private String name;
/** 父书籍 */
private mypack.Books parentCategory;
/** 子集合 */
private Set childCategories;
/** 完整构造函数 */
public Books(String name, mypack.Books parentCategory, Set childCategories) {
this.name = name;
this.parentCategory = parentCategory;
this.childCategories = childCategories;
}
/** 最小构造函数 */
public Books(Set childCategories) {
this.childCategories = childCategories;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public mypack.Books getParentCategory() {
return this.parentCategory;
}
public void setParentCategory(mypack.Books parentCategory) {
this.parentCategory = parentCategory;
}
public Set getChildCategories() {
return this.childCategories;
}
public void setChildCategories(Set childCategories) {
this.childCategories = childCategories;
}
}
2. 映射文件,放在classes/mypack下
Books.hbm.XML
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping >
<class name="mypack.Books" table="books" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id>
<property name="name" type="string" >
<column name="NAME" length="15" />
</property>
<set
name="childCategories"
cascade="save-update"
inverse="true"
>
<key column="CATEGORY_ID" />
<one-to-many class="mypack.Books" />
</set>
<many-to-one
name="parentCategory"
column="CATEGORY_ID"
class="mypack.Books"

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:使用tomcat4.1.31和mysql 配置数据源

下一篇:[Portal参考手册]目录