Can properties mapped in hbm.xml be transient?
Asked Answered
I

3

6

Suppose I have a User entity like this:

class User {
    private String login;
    transient private String hashedPassword;
}

I don't want to ever transfer hashedPassword to clients, so I make it transient.

This class is mapped by Hibernate, with both fields mapped in hbm.xml.

Is this implementation safe and correct? Will Hibernate correctly store hashedPassword in database, load it into objects from database, keep it in replicated 2nd level cache and local session cache etc?

In order words, does Hibernate or 2nd level cache respect transient in any way or completely ignore it?

EDIT: I already got two answers that didn't seem to include one specific aspect of the equation. I am not using annotations at all, only XML mappings in hbm.xml. And this Java-transient field is OR-mapped in hbm.xml.

Idem answered 28/12, 2011 at 18:10 Comment(0)
P
9

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.

Perdue answered 28/12, 2011 at 18:19 Comment(3)
Thank you, but I specifically mentioned that the field is mapped in hbm.xml. I'm not suing annotations at all.Idem
Yes thats true- the POJO annotation stuff is irrelevant. I added a bullet to address your question. In any case - its simple, just leave the variable out of your map - and hibernate will look the other way. No saving or caching will be done, because hibernate's "map" of your object will not include the unmapped variable.Perdue
Thanks jay, but my point is quite opposite. I am asking whether it is always safe, 2nd lv cache and whatnot, to have a transient POJO field mapped with hbm.xml.Idem
F
3

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."

Fiorenza answered 28/12, 2011 at 18:19 Comment(2)
Thank you, but I specifically mentioned that the field is mapped in hbm.xml. I'm not suing annotations at all.Idem
Does Hibernate actually map the property in that case? I was under the assumption that annotations and hbm.xml behaved the same, but perhaps that's not the case. Since transient will ensure that the property is not serialized, is your question entirely about Hibernate behavior?Fiorenza
M
0

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.

Manse answered 28/12, 2011 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.