Property Mappings
So far, we have only shown how to specify column mappings for simple
primitive types. There are still a few bits of metadata that you can use
to fine-tune your mappings. In this section, you’ll learn more annotations
for more complex property mappings. Java Persistence has mappings for JDBC
Blob
s and Clob
s, serializable objects, and embeddable
objects, as well as optimistic concurrency with version properties. We
discuss all of these.
@Transient
In our first example of our Employee
bean class, we showed that the
persistence manager would assume that every nontransient property
(getter/setter or field,
depending on your access type) in your bean class is persistent, even if
the property does not have any mapping metadata associated with it. This
is great for fast prototyping of your persistent objects, especially
when your persistence vendor supports autotable generation. However, you
may have properties that you don’t want to be persistent, and therefore
this default behavior is inappropriate. For instance, let’s assume we
want to express what an employee is currently doing without tying this
information to persistent storage. We may very simply declare:
/**
* Description of what the Employee's currently
* working on. We don't need to store this in the DB.
*/
@Transient
// Don't persist this
private String currentAssignment;
When you annotate a property with @javax.persistence.Transient
, the persistence
manager ignores it and doesn’t treat it as persistent.
@Basic and ...
Get Enterprise JavaBeans 3.1, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.