2009年6月26日 星期五

hibernate order by

假設 Account 的主鍵是 username VARCHAR,如果想要列出所有帳號資料,並依主鍵排序的話。
可以使用 id 進行排序即可,不一定要指定 username,如:

from Account as acc order by acc.id

這樣的寫法等同於

from Account as acc order by acc.username

但由於以 id 排序,不需要知道特定表格的主鍵 column 的名稱,
在某些無法得知 column 名稱的場合,可以派得上用場

2009年6月19日 星期五

hibernate 關聯 hbm.xml 設定 範例

many-to-one範例 -
<many-to-one name="targetGroup" column="group_id" class="com.transtep.green.servlet.framework.hibernate.GreenGroup" insert="false" update="false" cascade="save-update" outer-join="true" />

one-to-many範例 -
<set name="relAccExtSet" cascade="all" inverse="true" lazy="true">
<key>
<column name="account_id" />
</key>
<one-to-many class="com.transtep.green.servlet.framework.hibernate.GreenAccountExt" />
</set>

one-to-one範例 -
<one-to-one name="relSauth" class="com.transtep.green.servlet.framework.hibernate.GreenSimpleAuth" cascade="all" property-ref="relAccount" />

<many-to-one name="relAccount" column="account_id" class="com.transtep.green.servlet.framework.hibernate.GreenAccount" insert="false" update="false" cascade="save-update" outer-join="true" unique="true"/>

2009年6月8日 星期一

Spring PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer 的作用是可以將 spring 的 bean-config.xml 裡的某些資料
抽離出來,放到另外一個 key-value 的 property file 裡,方便統一設定。
先在bean-config.xml裡加入:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:green-setting.properties</value>
</property>
</bean>

再建立對應的 green-setting.properties 檔:
com.transtep.green.db.jdbcurl=jdbc:postgresql://db.transtep.com:5432/MARK_GREEN?charSet=utf8
com.transtep.green.db.user=postgres
com.transtep.green.db.password=admin

如此一來就可以在bean-config.xml裡使用${key}變數來設定資料,如:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>org.postgresql.Driver</value>
</property>
<property name="jdbcUrl">
<value>${com.transtep.green.db.jdbcurl}</value>
</property>
<property name="user">
<value>${com.transtep.green.db.user}</value>
</property>
<property name="password">
<value>${com.transtep.green.db.password}</value>
</property>
略....

2009年6月7日 星期日

debian 下的啟動服務設定 script update-rc.d

在 /etc/init.d 中建立一個叫作 zope 的 script , 然後

update-rc.d zope defaults

就會產生以下連結::

Adding system startup for /etc/init.d/zope ...
/etc/rc0.d/K20zope -> ../init.d/zope
/etc/rc1.d/K20zope -> ../init.d/zope
/etc/rc6.d/K20zope -> ../init.d/zope
/etc/rc2.d/S20zope -> ../init.d/zope
/etc/rc3.d/S20zope -> ../init.d/zope
/etc/rc4.d/S20zope -> ../init.d/zope
/etc/rc5.d/S20zope -> ../init.d/zope

2009年6月6日 星期六

hql 的left outter join轉換成sql的結果

hql:
from TpmaAccount as a
left join a.relTpmaStaff as staff with staff.projectId = 1

sql:
select
tpmaaccoun0_.username as username0_0_,
tpmastaff1_.staff_id as staff1_10_1_,
tpmaaccoun0_.password as password0_0_,
tpmaaccoun0_.role as role0_0_,
tpmaaccoun0_.enabled as enabled0_0_,
tpmaaccoun0_.valid_bdate as valid5_0_0_,
tpmaaccoun0_.valid_edate as valid6_0_0_,
tpmaaccoun0_.staff_id as staff7_0_0_,
tpmaaccoun0_.login_date as login8_0_0_,
tpmaaccoun0_.login_cnt as login9_0_0_,
tpmaaccoun0_.login_ip as login10_0_0_,
tpmastaff1_.project_id as project2_10_1_,
tpmastaff1_.first_name as first3_10_1_,
tpmastaff1_.last_name as last4_10_1_,
tpmastaff1_.sex as sex10_1_,
tpmastaff1_.org as org10_1_,
tpmastaff1_.cellphone as cellphone10_1_,
tpmastaff1_.telephone as telephone10_1_,
tpmastaff1_.address as address10_1_,
tpmastaff1_.skype as skype10_1_,
tpmastaff1_.msn as msn10_1_,
tpmastaff1_.email as email10_1_,
tpmastaff1_.remark as remark10_1_
from
public.tpma_account tpmaaccoun0_
left outer join
public.tpma_staff tpmastaff1_
on tpmaaccoun0_.staff_id=tpmastaff1_.staff_id
and (
tpmastaff1_.project_id=1
)