hibernate 属性 能否既持久又计算?
Can hibernate property be both persistent and calculated?
假设我有一个具有 dateOfBirth
和 yearOfBirth
属性的 Person
class,保存在 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
列肯定存在于 table.
中
不仅如此,我尝试使用简化映射:
<property name="yearOfBirth" type="integer" formula="(
TO_NUMBER(TO_CHAR(dateofbirth, 'YYYY'), 'FM9999')
)" generated="always">
<column name="year_of_birth"/>
</property>
它给了我正确的输出,但我的数据库中的列在实体更新后仍然是空的。我认为 generated="always"
部分应该制作 属性 updatable.
所以,谁能告诉我,是否有正确的方法可以使 属性 既计算又持久?
Computed properties do not have a column mapping of their own.
已经讨论过here
假设我有一个具有 dateOfBirth
和 yearOfBirth
属性的 Person
class,保存在 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
列肯定存在于 table.
不仅如此,我尝试使用简化映射:
<property name="yearOfBirth" type="integer" formula="(
TO_NUMBER(TO_CHAR(dateofbirth, 'YYYY'), 'FM9999')
)" generated="always">
<column name="year_of_birth"/>
</property>
它给了我正确的输出,但我的数据库中的列在实体更新后仍然是空的。我认为 generated="always"
部分应该制作 属性 updatable.
所以,谁能告诉我,是否有正确的方法可以使 属性 既计算又持久?
Computed properties do not have a column mapping of their own.
已经讨论过here