【发布时间】:2015-02-25 13:35:36
【问题描述】:
假设我有一个具有dateOfBirth 和yearOfBirth 属性的Person 类,持久保存在Postge 数据库中。
我需要映射yearOfBirth 条件:如果设置了dateOfBirth,则从中提取年份,否则使用yearOfBirth 值。
我尝试使用这样的映射:
<property name="dateOfBirth" type="timestamp" >
<column name="date_of_birth" />
</property>
<property name="yearOfBirth" type="integer" formula="(
CASE
WHEN dateofbirth is NULL THEN year_of_birth
ELSE TO_NUMBER(TO_CHAR(dateofbirth, 'YYYY'), 'FM9999')
END
)" generated="always">
<column name="year_of_birth"/>
</property>
但它给了我 PSQLException:ERROR: column coreemploy0_.year_of_birth does not exist。但是表中肯定存在列year_of_birth。
不仅如此,我还尝试使用简化映射:
<property name="yearOfBirth" type="integer" formula="(
TO_NUMBER(TO_CHAR(dateofbirth, 'YYYY'), 'FM9999')
)" generated="always">
<column name="year_of_birth"/>
</property>
它给了我正确的输出,但更新实体后我的数据库中的列仍然为空。我认为generated="always" 部分应该使属性可更新。
那么,谁能告诉我,有没有一种正确的方法可以使该属性既计算又持久?
【问题讨论】:
标签: hibernate postgresql hibernate-mapping