xml 代码
- CREATE TABLE CUSTOMER (
- ID int(20) NOT NULL,
- primary key (ID)
- )
- CREATE TABLE SHOPPING_CART (
- ID int(20) NOT NULL,
- TOTAL integer,
- primary key (ID)
- )
- ALTER TABLE SHOPPING_CART ADD CONSTRAINTS FK_CUSTOMER__SHOPPING_CART FOREIGN KEY (ID) REFERENCES CUSTOMER
xml 代码
- <hibernate-mapping package="com.wangtong.hibernate.one2one.pojo">
- <class name="Topic" dynamic-update="true" dynamic-insert="true" table="Topic" >
- <id name="id" type="integer" column="id">
- <generator class="identity" />
- </id>
- <property name="name" column="name" type="string" not-null="true" length="50" />
- <property name="total" column="total" type="int" not-null="true" length="50" />
- <!-- 一对一关系连接 -->
- <one-to-one name="shoppingCart" class="ShoppingCart" cascade="delete" outer-join="auto" constrained="false"/>
- </class>
- </hibernate-mapping>
java 代码
- public class Customer {
- private int id;
- private String name;
- private ShoppingCart shoppingCart;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- public ShoppingCart getShoppingCart() {
- return shoppingCart;
- }
- public void setShoppingCart(ShoppingCart shoppingCart) {
- this.shoppingCart = shoppingCart;
- }
- }
xml 代码
- <hibernate-mapping package="com.wangtong.hibernate.one2one.pojo">
- <class name="Topic" dynamic-update="true" dynamic-insert="true" table="Topic" >
- <id name="id" type="integer" column="id">
- <generator class="foreign">
- <param name="property">customer</param>
- </generator>
- </id>
- <property name="name" column="username" type="string" not-null="true" length="50" />
- <!-- 一对一关系连接 -->
- <one-to-one name="customer" class="Customer" cascade="none" outer-join="auto" constrained="true"/>
- </class>
- </hibernate-mapping>
java 代码
- public class ShoppingCart {
- private int id;
- private int total;
- private Customer customer;
- public Customer getCustomer() {
- return customer;
- }
- public void setCustomer(Customer customer) {
- this.customer = customer;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getTotal() {
- return total;
- }
- public void setTotal(int total) {
- this.total = total;
- }
- }