Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can properties mapped in hbm.xml be transient?

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.

like image 274
Konrad Garus Avatar asked Dec 28 '11 18:12

Konrad Garus


People also ask

Which element of HBM XML is used to map a Java Util set property in hibernate?

A - The <property> element is used to map a Java class property to a column in the database table. B - The name attribute of the element refers to the property in the class.

Does hibernate allow multiple class mappings in one mapping file?

The hibernate-mapping element allows you to nest several persistent <class> mappings, as shown above. It is, however, good practice (and expected by some tools) to map only a single persistent class, or a single class hierarchy, in one mapping file and name it after the persistent superclass.

What is transient annotation in hibernate?

@Transient annotation in JPA or Hibernate is used to indicate that a field is not to be persisted or ignore fields to save in the database. @Transient exist in javax. persistence package. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.

What is hibernate HBM XML?

The mapping document is an XML document having <hibernate-mapping> as the root element, which contains all the <class> elements. The <class> elements are used to define specific mappings from a Java classes to the database tables.


2 Answers

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.

like image 153
jayunit100 Avatar answered Sep 30 '22 04:09

jayunit100


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

like image 23
wrschneider Avatar answered Sep 30 '22 04:09

wrschneider