SSH整合中,有了多對一後,很多朋友插入對象的時候都覺得很麻煩,下面給大家介紹一個很方便實用的方法,來簡化大家的操作。來看下面這樣一個例子 :
新聞類(其中新聞和類別形成了多對一的關系)。
package com.qianyan.model;
public class Question{
private Long id;
private String title;
private String answer;
private Long catelogId; //簡單之處在於添加此字段
private Catelog catelog;
get,set();
}
下面看配置文件:Question.hbm.xml
<?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="com.qianyan.model.Question" table="question">
<id name="id" type="java.lang.Long">
<generator class="identity"/>
</id>
<property name="title">
<column name="title" length="100"></column>
</property>
<property name="answer">
<column name="answer" length="2000"></column>
</property>
<!-- 對應同一column="cateloigId" -->
<property name="catelogId" type="java.lang.Long">
<column name="catelogId"></column>
</property>
<!-- 對應同一column="cateloigId" -->
<many-to-one name="catelog" column="catelogId" insert="false" update="false" lazy="false"></many-to-one>
</class>
</hibernate-mapping>