網頁

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

2013年9月23日 星期一

Hibernate學習筆記 - Error for: id numeric(19,2) identity not null

在用ant執行hbm2ddl來建table時,出現個問題,id的type設成big_integer,sql產生出一段底下的sql

id numeric(19,2) identity not null

本以為export出sql且ant回應build success是ok的,但發現table根本沒有create,將sql整個執行過後發現,sybase(可能其他DB也是有)要求numeric(19,0)才能正常運作,我針對id的宣告如下:

<id name="id" column="id" type="big_integer">
    <meta attribute="field-description">xxx</meta>
    <meta attribute="use-in-tostring">true</meta>
    <meta attribute="use-in-equals">true</meta>
    <generator class="native" />
</id>


想說就直接加成這樣,將precision設成10,scale設成0

<id name="id" type="big_integer">
    <column name="id" precision="10" scale="0" />
    <meta attribute="field-description">xxx</meta>
    <meta attribute="use-in-tostring">true</meta>
    <meta attribute="use-in-equals">true</meta>
    <generator class="native" />
</id>


但~ 不能用,找了很久的資料都找不到,結果跟generator一樣,是順序問題,如下設定就好了

<id name="id" type="big_integer">
    <meta attribute="field-description">xxx</meta>
    <meta attribute="use-in-tostring">true</meta>
    <meta attribute="use-in-equals">true</meta>
    <column name="id" precision="10" scale="0" />
    <generator class="native" />
</id>


參考網址:
Chapter 5. Basic O/R Mapping
Re: want to force scale=0 attr in hbm for BigInteger

2013年8月29日 星期四

Spring、J2EE、Hibernate學習筆記-could not initialize proxy - no Session

記錄一種狀況,在spring中使用hibernate是很常有的事,尤其在hbm中將兩個table的資料做聯集,不過遇到個狀況,記錄如下:

先取得人員的資訊(其中聯集到此人員的角色資訊),但事後要用時卻出現Exception如下:
could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session


看起來是預設聯集另一個table時是用lazy的方式,加上用的是spring的
HibernateTemplate,會自動close session,在hbm file那個field的property attribute加上lazy="false",這樣就ok了

2013年8月13日 星期二

Hibernate學習筆記 - org.hibernate.MappingException: No Dialect mapping for JDBC type: -1

試了半天也找了很多的資料,就是看不到有什麼解釋,只知道在設定hbm中設定property的type時,若使用的是sybase資料庫,type設定成text會有問題,會一直出現下列的Exception:
org.hibernate.MappingException: No Dialect mapping for JDBC type: -1

猜就是type不支援。查了資料,大多都說Text是varchar(4000),要用就type="string" length="4000",這樣設定就過了,不過4000也太大了吧