2009年7月29日 星期三

GWT : cannot be resolved to a type

Compile error:
[ERROR] Line 7: The import com.abc.admin.orm.Admin cannot be resolved
[ERROR] Line 86: Admin cannot be resolved to a type

GWT 會依 .gwt.xml 中的 <source> tag 找檔案;
<module>
<source path="client"/>
<source path="orm"/>
....
</module>

但是 source 要和 .gwt.xml 檔在同一個 package 下.
例 : 如果此 .gwt.xml 為 /com/abc/app/gwt/Sample.gwt.xml , GWT 讀取的 Classes 必須在 package com.abc.app.gwt.client 或 com.abc.app.gwt.orm 之下.

如果想引用不同 package 的 classes, 可用 <inherits> tag :

1. /com/abc/app/gwt/Sample.gwt.xml --

<module>
<source path="client"/>
<source path="orm"/>

<inherits name="com.abc.admin.Admin"/>

<entry-point class="com.abc.app.gwt.client.SampleGWTEntry"/>

....
</entry-point>

2. /com/abc/admin/Admin.gwt.xml --

<module>
<source path="client"/>
<source path="orm"/>
</module>

此 module 不需 <entry-point>

** com.abc.app.gwt.client.SampleGWTEntry 就可使用 com.abc.admin.orm 下的 classes了.


Reference:
http://code.google.com/p/google-web-toolkit-incubator/wiki/PathsHandlingFAQ

2009年7月24日 星期五

Hibernate : "Found shared references to a collection"

新增資料時發生錯誤 : "Found shared references to a collection".

原因:

新增的物件是由 已存在的物件複製 , 再進行 entityManager.persist.
但 EntityManage.persist 並不會更新 one-to-many or many-to-many 的 collection reference.

BeanUtils.copyProperties and PropertyUtils.copyProperties(newPO, refPO)是淺拷貝,導致 newPO 與 refPO 這兩個物件引用的Collection是同一個Collection,這在hibernate中是不允許的,參見Hibernate reference第6章的"Two entities may not share a reference to the same collection instance"。
這種問題常見於複製時。

Solution:

newP.setxxxCollection(null) 或不要用 BeanUtils.copyPropertis 而是直接set values , depends on case.
雖然在 EntityManager.persist(newPO) 之後 EntityManager.refresh(newPO) 也可以避免這個 Exception, 但是 refresh 卻在某些狀況會發生其他的 Exception ;

Reference:
http://www.blogjava.net/leekiang/archive/2008/10/31/237908.html


Hibernate : NonUniqueObjectException

更新資料時出現錯誤訊息 :
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.jida.surgery.orm.Application#8]

Solution:
原本用 session.update(entity) , 改為 session.merge(entity) 就 OK 了.

Reference:
http://hi.baidu.com/limeng1028/blog/item/275fbd12200544cdc2fd7828.html