463
技術社區[雲棲]
hibernate中關聯關係的小結
一對一方案1:一對一的表現是形式是建立在一對多的基礎之上。讓外鍵唯一就可以變成一對一
類的寫法:
Users類
public class Users{
private Long userid;
private String username;
//看著是一對多。實質由於外鍵放已經具備唯一約束,所以set集合中隻有一條數據
private Set<Orders> orderses = new HashSet<Orders>();
}
Orders類:
public class Orders{
private Long orderid;
private Double price;
private Users users;
}
配置文件:
Users配置文件:
<hibernate-mapping>
<class name="com.puke.domain.Users" table="USERS" schema="PUKE12">
<id name="userid" type="java.lang.Long">
<column name="USERID" precision="22" scale="0" />
<generator />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="40" />
</property>
<!-- set標簽的作用:告知Hibernate在當前一方中關聯多方的那個集合對象的名字 -->
<set name="orderses" inverse="true" >
<!-- key column 告知hibernate表中的外鍵是誰 -->
<key>
<column name="USER_ID" precision="22" scale="0" unique="true" />
</key>
<!--
one-to-many 這個標簽的含義:
1,告知hibernate當前user與order的關係
2,告知hibernate當前多方的class標簽name的值,獲取這個class節點中的table屬性的值,這樣hibernate就知道當前的
USER_ID位於哪個表中。
3,告知bibenrate當前set集中放存放什麼對象
-->
<one-to-many />
</set>
</class>
</hibernate-mapping>
Orders配置文件:
<hibernate-mapping>
<class name="com.puke.domain.Orders" table="ORDERS" schema="PUKE12">
<id name="orderid" type="java.lang.Long">
<column name="ORDERID" precision="22" scale="0" />
<generator />
</id>
<many-to-one name="users" fetch="select">
<column name="USER_ID" precision="22" scale="0" unique="true" />
</many-to-one>
<property name="price" type="java.lang.Double">
<column name="PRICE" precision="6" />
</property>
</class>
</hibernate-mapping>
一對一的方案2:
一對一的表現形式就是一對一
Users類:
public class Users{
private Long userid;
private String username;
private Orders orders;
}
Orders類:
public class Orders{
private Long orderid;
private Double price;
private Users users;
}
配置文件:
Users配置文件:
<hibernate-mapping>
<class name="com.puke.domain.Users" table="USERS" schema="PUKE12">
<id name="userid" type="java.lang.Long">
<column name="USERID" precision="22" scale="0" />
<generator />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="40" />
</property>
<!-- set標簽的作用:告知Hibernate在當前一方中關聯多方的那個集合對象的名字 -->
<one-to-one name="orders" ></one-to-one>
</class>
</hibernate-mapping>
Orders配置文件:
<hibernate-mapping>
<class name="com.puke.domain.Orders" table="ORDERS" schema="PUKE12">
<id name="orderid" type="java.lang.Long">
<column name="ORDERID" precision="22" scale="0" />
<generator />
</id>
<many-to-one name="users" fetch="select">
<column name="USER_ID" precision="22" scale="0" unique="true" />
</many-to-one>
<property name="price" type="java.lang.Double">
<column name="PRICE" precision="6" />
</property>
</class>
</hibernate-mapping>
一對多:
類的寫法:
一方:Users
public class Users{
private Long id;
private String username;
private Set<Orders2> orders2s = new HashSet<Orders2>();
}
多方:Orders
public class Orders{
private Long id;
private Double price;
private Users2 users2;
}
配置文件:
一方的配置文件:
<hibernate-mapping>
<class name="com.puke.domain2.Users2" table="USERS2" schema="PUKE12">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="40" />
</property>
<set name="orders2s" inverse="true">
<key>
<column name="USERS_ID" precision="22" scale="0" />
</key>
<one-to-many />
</set>
</class>
</hibernate-mapping>
多方配置文件:
<hibernate-mapping>
<class name="com.puke.domain2.Orders2" table="ORDERS2" schema="PUKE12">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator />
</id>
<many-to-one name="users2" fetch="select">
<column name="USERS_ID" precision="22" scale="0" />
</many-to-one>
<property name="price" type="java.lang.Double">
<column name="PRICE" precision="6" />
</property>
</class>
</hibernate-mapping>
多對多;
最後更新:2017-04-03 14:53:43
上一篇:
Java泛型--泛型入門
下一篇:
擴展歐幾裏得求兩多項式最大公因式
解決Setting property 'source' to 'org.eclipse.jst.jee.server的問題
winform程序textbox滾動條保持在最下麵 內容不閃爍
態勢感知將DDOS扼殺在搖籃中:涉及數千台PostgreSQL服務器
解決錯誤ERROR: No suitable Java found. In order to properly use the Android Developer Tools,…
這些年,聯想未能實現的4個戰略轉型
Spring-Bean的初始化(init方法和實現org.springframework.beans.factory.InitializingBean接口)
[.NET 基於角色安全性驗證] 之三:ASP.NET Forms 身份驗證
唱見散搭:在聲線中融化的溫暖心境
hadoop 2.7.3 集群搭建遇到問題以及解決
學界 | ICLR 2018截稿:取消公開評審改為雙盲引人關注