---
title: "Hibernate"
author: "winyee"
date: "2010-01-24T16:27:29Z"
tags: []
categories: ["企业架构"]
slug: "hibernate"
draft: false
lastmod: "2010-01-24T16:27:29Z"
id: 578465
url: "https://winyee.iteye.com/blog/578465"
---

User <-> Address:

Address是User的子对象。OneToOne is set on address field in User entity.

case 1. 没有设置CascadeType, only persist User: -> TransientException will be thrown, it said there are transient object.

case 2. CascadeType is not set, persist User, persist Address -> successful. sql: 1.intert user; 2.insert adress; 3.update user with address id. step 3 should not be executed. SEQUENCE of actions is important. 

case 3. CasacdeType is not set. persist Address, persist User -> ok, sql: 1.insert Address; 2.insert User. it is ok.

case 4. CascadeType is PERSIST. persist User -> ok. sql:1.insert Address; 2.insert User.

case 5. CascadeType is PERSIST. persist User, Persist Address or Persist Address, Persist User -> ok. sql: 1.insert Address; 2.insert User.