Can properties mapped in hbm.xml be transient?

天涯浪子 提交于 2019-12-04 08:34:15

Unmapped/Transient properties are not saved by hibernate.

Hibernate understands the significance of standard java transient modifiers - but also allows you to annotate properties as transient using the @Transient annotation, if you so choose... Or just leave the field out of your mapping file altogether.

In your case, you probably will NOT need to do anything special, hibernate should simply "do the right thing", by ignoring unmapped fields.

So : the lesson learned here -

If only using hbm.xml

1) Unmapped properties are not saved by hibernate - they are effectively transient.

If using POJOs

2) Hibernate will ignore saving "@Transient" annotated variables :

@Transient
int ignored=0;

3) Hibernate will also ignore saving variables with standard "transient" modifiers :

private transient int ignored =0;

See http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/ for an excellent explanation of this.

wrschneider

It looks like Hibernate will not persist a field with the transient keyword, regardless of what other annotations you have.

The separate @Transient annotation will allow you to direct Hibernate to ignore a non-transient field for persistence, but I don't think it's possible to do the opposite of having Hibernate persist a transient field.

Similar discussion here:

JPA - using annotations in a model

Annotation @Basic to transient variables

The most relevant quote via above, from JPA 2.0 spec: "Mapping annotations must not be applied to fields or properties that are transient or @Transient."

Here is what I think - Hibernate is just a mapping technology. When you mark a field as TRANSIENT it will not be persisted by java. And since its state is not persisted why should hibernate maintain it in the L2 cache, etc ? so hibernate should have no problem even if you map the transient field in hbm file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!