顯示具有 JPA 標籤的文章。 顯示所有文章
顯示具有 JPA 標籤的文章。 顯示所有文章

星期五, 5月 08, 2009

Hibernate初體驗

資料庫一向是我特別有興趣的部分, 但Hibernate卻一直沒好好研究與使用, 除了是對於自己既有技能的依賴, 另外也是不想因為framework而失去某些彈性. 但現在Hibernate支援了JPA(EJB3.0規格中的重點之一), 所以系統若需要分散式架構時也可以很容易的擴充, 另外JPA也可以使用在GAE上, 因此現在學習使用Hibernate可說是非常有利的時機點.

我先閱覽了 良葛格學習筆記, 受益良多, 也對於Hibernate有一個快速的認識 (這幾年來, 我應該是第四次看這個網站上的Hibernate介紹了, 站主真的很用心, 更新的也很快).

不過接下來的實作練習還是吃了不少苦頭, 因為
  • 我不喜歡重頭開始刻程式, 所以看到Hibernate Tool可以從資料庫反轉產生code就覺得非用不可
  • 我並不想認識原來的Hibernate, 也就是還需要*.hbm.xml的寫法
  • Hibernate的JPA實作包括 Annotations 和 EntityManager, 我並不清楚這兩者的關係以及與EJB的關聯性
  • 我也沒搞懂persistence.xml 和 hibernate.cfg.xmlx兩者的關係以及與EJB的關聯性, 連要放哪個目錄下都不知道
  • 一堆相依的libraries也沒搞懂
  • 因為需求的關係, 目前我只想使用Tomcat, 並不想使用JBoss/ Glassfish
  • 我不想使用command line來跑Ant之類的東西, 一心只想使用Eclipse來操作
  • 我甚至也不知道Hibernate可以不在AP server/ Web server跑, 哈哈
  • 我一向沒看完使用者手冊的習慣... 真糟糕的習慣...
綜合以上就讓我吃足了苦頭, 雖然後來都搞清楚了, 也發現在良葛格學習筆記中其實大部分都已說得很清楚, 不過這樣try-error的過程中也算是快速累積經驗吧, 哈哈...

另外試著跑自己想要的SQL statement和子查詢, 結果也是ok的.

初體驗的結論是, 目前看來一切都很不錯. 要繼續深入研究下去囉!

星期一, 5月 04, 2009

關於GAE中用來儲存資料的兩個標準 (JDO, JPA)

話說昨天看了一下GAE的說明得知, 若要在GAE上儲存資料必須要使用JDO或JPA這兩種技術... 但是其實這兩種技術我都不會, 哈哈~ 一直都還在用很老舊的架構 Session Bean <-> DAO <-> JDBC <-> DB, 因為我喜歡使用很華麗的SQL statement來提高資料存取的速度, 曾經我也使用過Entity Bean來開發J2EE的系統, 那根本是場災難... 也造成我一直對於所謂的frameworks有點敬而遠之 (Struts, Webwork, Spring, Hibernate... etc), 且也不希望增加版本更新的困擾 (光Tomcat要不要upgrade都可以讓我考慮好幾天), 因此都只有大概看看文件, 了解一下這些frameworks存在的必要性而已.

Anyway, 在GAE上儲存資料必須要使用JDO或JPA這兩種標準, 所以我今天花了點時間研究一下, Google上是採用 Datanucleus的solution, 但這時還不曉得這個solution是甚麼, 接著上網查到 http://en.wikipedia.org/wiki/Datanucleus中有這一段: DataNucleus Access Platform is the persistence layer behind Google App Engine for Java. 原來這個 plateform同時支援JDO跟JPA兩個標準, 因此想用哪個就用哪個囉.

另外也看到Hibernate主要是JPA的實作, 因此若使用JPA可算是有摸蜆仔兼洗褲的作用. 以後若有機會使用Hibernate的話, 不妨就使用JPA來儲存資料吧.

不過看到以下那一堆unsupported features... 很多features我看不太懂是甚麼意思, 不過不能join不能aggregrate?! 不會吧~~ 那我要資料總筆數怎麼作咧... 還是說有method可以用? 看來只好等空間拿到手再來好好試用一下.

Edit 1: 剛又多研究了一下, 知道資料總筆數要用 datastore.prepare(query).countEntities()來取得, 也就是說, 基本上資料應該已經全部從資料庫給拉出來了, 這樣若有需要作報表系統的話, 效能堪慮阿...

Unsupported Features of JDO

The following features of the JDO interface are not supported by the App Engine implementation:

  • Unowned relationships. You can implement unowned relationships using explicit Key values. JDO's syntax for unowned relationships may be supported in a future release.
  • Owned many-to-many relationships.
  • contains() syntax for query filters on Collection fields. You can test that a multi-valued property (a Collection field) has a value using an equality filter: collection == "value"
  • "Join" queries. You cannot use a field of a child entity in a filter when performing a query on the parent kind. Note that you can test the parent's relationship field directly in query using a key.
  • JDOQL grouping and other aggregate queries.
  • Polymorphic queries. You cannot perform a query of a class to get instances of a subclass. Each class is represented by a separate entity kind in the datastore.
  • IdentityType.DATASTORE for the @PersistenceCapable annotation. Only IdentityType.APPLICATION is supported.
  • There is currently a bug preventing preventing persistent fields on superclasses from being saved to the datastore. This will be fixed in a future release.


Unsupported Features of JPA

The following features of the JPA interface are not supported by the App Engine implementation:

  • Owned many-to-many relationships, and unowned relationships. You can implement unowned relationships using explicit Key values, though type checking is not enforced in the API.
  • "Join" queries. You cannot use a field of a child entity in a filter when performing a query on the parent kind. Note that you can test the parent's relationship field directly in query using a key.
  • Aggregation queries (group by, having, sum, avg, max, min)
  • Polymorphic queries. You cannot perform a query of a class to get instances of a subclass. Each class is represented by a separate entity kind in the datastore.