论坛(主题Topic和回复Reply继承文章Article)
Article.java
package com.domain;
public class Article {
private Integer id;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
Topic.java
package com.domain;
public class Topic extends Article{
private Integer type;
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
}
Reply.jva
package com.domain;
public class Reply extends Article{
private Integer level;
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
}
1、所有的数据保存在一张表的情况(Article.hbm.xml)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.domain">
<class name="Article" table="article" discriminator-value="article">
<id name="id">
<generator class="native"/>
</id>
<discriminator column="kind" type="string"></discriminator>
<property name="content" column="content"/>
<subclass name="Topic" discriminator-value="topic">
<property name="type"></property>
</subclass>
<subclass name="Reply" discriminator-value="reply">
<property name="level"></property>
</subclass>
</class>
</hibernate-mapping>
2、三张表的情况
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.domain">
<class name="Article" table="article">
<id name="id">
<generator class="native"/>
</id>
<property name="content" column="content"/>
<joined-subclass name="Topic" table="topic">
<key column="id"></key>
<property name="type"></property>
</joined-subclass>
<joined-subclass name="Reply" table="reply">
<key column="id"></key>
<property name="level"></property>
</joined-subclass>
</class>
</hibernate-mapping>
3、两张表的情况
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.domain">
<class name="Article" abstract="true">
<id name="id">
<generator class="increment"/>
</id>
<property name="content" column="content"/>
<union-subclass name="Topic" table="topic">
<property name="type"></property>
</union-subclass>
<union-subclass name="Reply" table="reply">
<property name="level"></property>
</union-subclass>
</class>
</hibernate-mapping>