一对一方案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 class="assigned" />
</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 class="com.puke.domain.Orders" />
</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 class="assigned" />
</id>
<many-to-one name="users" class="com.puke.domain.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 class="assigned" />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="40" />
</property>
<!-- set标签的作用:告知Hibernate在当前一方中关联多方的那个集合对象的名字 -->
<one-to-one name="orders" class="com.puke.domain.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 class="assigned" />
</id>
<many-to-one name="users" class="com.puke.domain.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 class="assigned" />
</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 class="com.puke.domain2.Orders2" />
</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 class="assigned" />
</id>
<many-to-one name="users2" class="com.puke.domain2.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>
多对多;
一对一的表现是形式是建立在一对多的基础之上。让外键唯一就可以变成一对一
类的写法:
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 class="assigned" />
</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 class="com.puke.domain.Orders" />
</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 class="assigned" />
</id>
<many-to-one name="users" class="com.puke.domain.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 class="assigned" />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="40" />
</property>
<!-- set标签的作用:告知Hibernate在当前一方中关联多方的那个集合对象的名字 -->
<one-to-one name="orders" class="com.puke.domain.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 class="assigned" />
</id>
<many-to-one name="users" class="com.puke.domain.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 class="assigned" />
</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 class="com.puke.domain2.Orders2" />
</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 class="assigned" />
</id>
<many-to-one name="users2" class="com.puke.domain2.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>
多对多;