0.注意unsaved-value是個很重要的屬性。Hibernate通過這個屬性來判斷一個對象應該save還是update,如果這個對象的id是unsaved-value的話,那說明這個對象不是 persistence object要save(insert);如果id是非unsaved-value的話,那說明這個對象是persistence object(數據庫中已存在),只要update就行了。
1、
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.xindeco.myregister.pojo.MyRegisterInfo column: password (should be mapped with insert="false" update="false")
出錯原因:1、數據庫的字段值和javaBean中的屬性類型不統一。對于基本類型,要用wrapper類型而不是primitive類型。2、hibernate的配置文件xxx.hbm.xml中的屬性配置不為空,而數據庫中的字段卻為空。3.兩個字段對應同一列,如:password 和repassword同時對應數據庫表中的password一列,同時update和insert都設為true。
xml文件如下:
??? <property name="password"
????????????????????????? type="java.lang.String"
????????????????????????? update="true"
????????????????????????? insert="true"
????????????????????????? access="property"
????????????????????????? column="password"
????????????????????????? length = "32"
????????????????????????? />
???????????????????????? <property name="repassword"
????????????????????????? type="java.lang.String"
????????????????????????? update="false"
????????????????????????? insert="false"
????????????????????????? access="property"
????????????????????????? column="password"
????????????????????????? length = "32"
????????????????????????? />
解決的方法:
將repassword的insert和update設為false。
2.
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
錯誤原因:
在application.xml文件中deleteRegister方法以delete開頭,并沒有被設定為可更改的,應如下設置:
?<!--為事物創建代理類,并指定方法的事物類型-->
? <bean id="baseTxProxy" lazy-init="true" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
??? <property name="transactionManager">
????? <ref bean="transactionManager"/>
??? </property>
??? <property name="transactionAttributes">
????? <props>
??????? <prop key="add*">PROPAGATION_REQUIRED</prop>
??????? <prop key="cancel*">PROPAGATION_REQUIRED</prop>
???????????</props>
??? </property>
? </bean>
加上一行
? <prop key="delete*">PROPAGATION_REQUIRED</prop>
3.session---connection
4.one-to-one小結:
one-to-one在hibernate中可以用來作為兩張表之間的主鍵關聯,這也是hibernate中主鍵關聯的一種用法,這樣在一張表中的ID,在生成另外一張表的同時回自動插入到相應的ID字段中去,相應的XML文件設置比較簡單,舉例如下:
????<!-- 建立一對一的到Address的映射,這個是寫在User的XML配置文件中的 -->
????<!--?相應的User bean(PO)中也要添加屬性 com.xx.Address??address-->
??? <one-to-one name="address" cascade="all" class="com.xx.Address"/>
???
???<!--?cascade的屬性設置不再重復了,可以查看hibernate文檔 -->
????<!-- 建立一對一的到User的映射,這個是寫在Address的XML配置文件中的 -->
????<!--?相應的Address bean(PO)中也要添加屬性 com.xx.User user--> -->
??? <one-to-one name="user" class="com.xx.User" constrained="true"/>
??????為了在Address中使用User中的主鍵ID值,我們需要設置Address中的主鍵生成規則,如下所示,采用foreign關鍵字
???<id column="ID" name="id" type="long" unsaved-value="0">
????? <generator class="foreign">
??????? <param name="property">user</param>?
????? </generator>
???</id>
??????這里需要注意的是property的屬性值必須與上面到User的映射所填寫的name屬性值一致,這樣就完成了one-to-one的映射關系。
上面的過程都很簡單,下面我來說說這里需要注意的地方:
??1.???在設置屬性ID的時候必須注意字段的長度,如筆者這樣使用oracle的sequence來生成ID,其長度有14位之長,則應選擇hibernate類型long,對應的實體中應選擇Long,這樣不會出現溢出的情況。
??2.???在測試的時候必須要注意這兩張表之間因為已經存在了一對一的關系,所以我們不能只寫
?????????user.setAddress(address);
?????????而忽略了
?????????address.setUser(user);
?????????這樣在做插入的時候會報出attempted to assign id from null one-to-one property: address的錯誤,這一點初學者會經常犯,筆者也是其中之一。
?3.???如果不寫cascade="all"或者寫成cascade="none"的話,即使你寫了
?????????user.setAddress(address);
?????????address.setUser(user);
???????也不會發生任何事情,只有user會被存儲。
4. one-to-one的效率問題-------one-to-one在查詢時,總是查出和主表關聯的表,而且one-to-one的lazy屬性只有false proxy no-proxy三種,沒有true。outer-join="false"也只是徒增查詢語句條數,把本來的一條sql語句變成多條。所以在one-to-one這種一對一的關系不是很強的情況下(one-to-one關系強即總是查出這所有的幾個關聯表),或者是在一張表中存在多個one-to-one的情況下,使用最好one-to-many來代替one-to-one。
?
?
關于父子關系(all-delete-orphan)
當關聯雙方存在父子關系,就可以在 set 處設定 cascade 為 all-delete-orphan
所謂父子關系,即指由父方控制子方的持久化圣明周期,子方對象必須和一個父方對象關聯。如果刪除父方對象,應該級聯刪除所有關聯的子方對象;如果一個子方對象不再和一個父方對象關聯,應該把這個子方對象刪除。
all-deleteorphan 的能力:
1. 當保存或更新父方對象時,級聯保存或更新所有關聯的子方對象,相當于 cascade 為 save-update
2. 當刪除父方對象時,級聯刪除所有關聯的子方對象,相當于 cascade 為 delete
3. 刪除不再和父方對象關聯的所有子方對象
解除父子關系的 java 語句例如:
customer.getOrders().remove(order);
order.setCustomer(null);
tx.commit();
如果 cascade 屬性取默認值 null,當解除父子關系時,會執行如下 sql:
update ORDER set CUSTOMER_ID=null where ID=2
如果要把它也刪掉,則設置:
<set?? name="orders"? cascade="all-delete-orphan"? inverse="true">
<key? column="CUSTOMER_ID"? />
<one-to-many? class="mypack.Order" />
再運行時就會執行:delete from ORDERS where CUSTOMER_ID=2? and ID=2;
cascade幾種取值:
save-update:?? 級聯保存(load以后如果子對象發生了更新,也會級聯更新). 但它不會級聯刪除
delete:????????????? 級聯刪除, 但不具備級聯保存和更新
all-delete-orphan: 在解除父子關系時,自動刪除不屬于父對象的子對象, 也支持級聯刪除和級聯保存更新.
all:??????????????????? ?級聯刪除, 級聯更新,但解除父子關系時不會自動刪除子對象.
delete-orphan:刪除所有和當前對象解除關聯關系的對象
none:...
級聯保存和級聯更新總是集合在一起的, 所以沒單獨的save 或 updata