1.建立項目。java
2.下載官方jar包。hibernate-distribution-3.5.6-Finalapi
3.導入須要的包。app
D:\jar\hibernate\hibernate-distribution-3.5.6-Final\hibernate3.jar D:\jar\hibernate\hibernate-distribution-3.5.6-Final\lib\required\* D:\jar\hibernate\hibernate-distribution-3.5.6-Final\lib\jpa\hibernate-jpa-2.0-api-1.0.0.Final.jar
4.運行項目發現有錯。框架
java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
導入另外一個包dom
D:\jar\slf4j-nop-1.5.8.jar
注意此時的包必須與 slf4j-api-1.5.8.jar 版本相對應。
fetch
對於外鍵關聯,存在多個外鍵表實體對應一個主鍵表實體。在項目中就是一對多關聯。例:ui
①汽車品牌表Brand對應的實體類:this
public class Brand { private int brandId; private String name; private int bIndex; private String memo; public int getBrandId() { return brandId; } public void setBrandId(int brandId) { this.brandId = brandId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getbIndex() { return bIndex; } public void setbIndex(int bIndex) { this.bIndex = bIndex; } public String getMemo() { return memo; } public void setMemo(String memo) { this.memo = memo; } }
②汽車系列表Series對應的實體類:spa
public class Series { private int seriesId; private String name; private int sIndex; private String memo; private Brand brand; public int getSeriesId() { return seriesId; } public void setSeriesId(int seriesId) { this.seriesId = seriesId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getsIndex() { return sIndex; } public void setsIndex(int sIndex) { this.sIndex = sIndex; } public String getMemo() { return memo; } public void setMemo(String memo) { this.memo = memo; } public Brand getBrand() { return brand; } public void setBrand(Brand brand) { this.brand = brand; } }
實際關係是一個品牌的汽車包含多個系列,所以存在多個系列從屬於一個品牌的狀況。故配置Series.hbm.xml以下:.net
<?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.server.doman"> <class name="Series"> <id name="seriesId" column="series_id" type="int"> <generator class="increment" /> </id> <!-- <property name="brandId" column="brand_id" type="int"></property> --> <property name="name" column="name" type="string"></property> <property name="sIndex" column="s_index" type="int"></property> <property name="memo" column="memo" type="string"></property> <!-- 多個系列可能從屬於一個品牌 name對應Series類的brand屬性,column對應Brand表的主鍵列--> <many-to-one name="brand" class="com.server.doman.Brand" column="Brand_id"/> </class> </hibernate-mapping>
注意:以上在Series類中有了brand類就不用添該表中的brand_id,不然就會報錯
Column 'Brand_id' specified twice
列Brand_id 實例化兩次。
3.1多對一
<set order-by="csid" name="categorySeconds" fetch="join"> <key column="cid"/> <one-to-many class="cn.itcast.shop.categorysecond.vo.CategorySecond"/> </set>
3.2一對多
<many-to-one name="category" fetch="join" class="cn.itcast.shop.category.vo.Category" column="cid"/>
3.3獲取數據
String hql = "from Category"; List<Category> list = this.getHibernateTemplate().loadAll(Category.class);
總結:在映射文檔中定義抓取策略時,只有經過get或load方法才生效。