爲了熟悉hibernate,這幾天做一個簡單的論壇來練手。
現將hibernate一對多總結如下:
比如:
一個版塊 對應 多個帖子。
pojo類:zone
public class Zone {
private long zoneId;
private String zoneName;
private String zoneDis;
private Date createTime;
private long topicNum;
private Set<Topic> topic = new HashSet<Topic>(); //主題
......
private long zoneId;
private String zoneName;
private String zoneDis;
private Date createTime;
private long topicNum;
private Set<Topic> topic = new HashSet<Topic>(); //主題
......
//get set方法省去
}
主題(topic):
public class Topic {
private long topicId;
private String topicTitle;
private String topicContent;
private Date sendTime;
private User user;
private long FeedNum;
private Zone zone;//板塊
......
/get set 方法省去
}
private long topicId;
private String topicTitle;
private String topicContent;
private Date sendTime;
private User user;
private long FeedNum;
private Zone zone;//板塊
......
/get set 方法省去
}
再看映射文件:
zone.hbm.xml:
<hibernate-mapping package="com.mln.platform.bean">
<class name="Zone" table="zone">
<id name="zoneId" column="zoneid" type="long">
<generator class="native"/>
</id>
<property name="zoneName" column="zonename" type="string"/>
<property name="zoneDis" column="zoneDis" type="string"/>
<property name="createTime" column="createTime" type="timestamp"/>
<property name="topicNum" column="topicNum" type="long"/>
<!-- inverse=true 說明不是主控方。一般多的一方爲主控方。-->
<set name="topic" table="topic" cascade="all" inverse="true">
<key column="zoneId" not-null="true"/>
<one-to-many class="Topic"/>
</set>
</class>
</hibernate-mapping>
<class name="Zone" table="zone">
<id name="zoneId" column="zoneid" type="long">
<generator class="native"/>
</id>
<property name="zoneName" column="zonename" type="string"/>
<property name="zoneDis" column="zoneDis" type="string"/>
<property name="createTime" column="createTime" type="timestamp"/>
<property name="topicNum" column="topicNum" type="long"/>
<!-- inverse=true 說明不是主控方。一般多的一方爲主控方。-->
<set name="topic" table="topic" cascade="all" inverse="true">
<key column="zoneId" not-null="true"/>
<one-to-many class="Topic"/>
</set>
</class>
</hibernate-mapping>
topic.hbm.xml:
<hibernate-mapping package="com.mln.platform.bean">
<class name="Topic" table="topic">
<id name="topicId" column="topicId" type="long">
<generator class="native"/>
</id>
<property name="topicTitle" column="topicTitle" type="string"/>
<property name="topicContent" column="topicContent" type="string"/>
<property name="sendTime" column="sendTime" type="timestamp"/>
<property name="FeedNum" column="FeedNum" type="long"/>
<many-to-one name="zone" class="Zone" fetch="select" >
<column name="zoneId" />
</many-to-one>
</class>
</hibernate-mapping>
<class name="Topic" table="topic">
<id name="topicId" column="topicId" type="long">
<generator class="native"/>
</id>
<property name="topicTitle" column="topicTitle" type="string"/>
<property name="topicContent" column="topicContent" type="string"/>
<property name="sendTime" column="sendTime" type="timestamp"/>
<property name="FeedNum" column="FeedNum" type="long"/>
<many-to-one name="zone" class="Zone" fetch="select" >
<column name="zoneId" />
</many-to-one>
</class>
</hibernate-mapping>
看個例子:
如要根據板塊id查找這個板塊下的所有帖子。看hql寫法:
from Topic topic inner join fetch topic.zone zone inner join fetch topic.user user where zone.zoneId=?
這樣可查出所需要的信息。,當然這個語句也可查出相關的用戶信息。
還有個簡單的方法也可以達到同樣的效果。那就是 設懶加載 lazy="false"