SCBCD Notes: Java Persistence (Part 4)
Published December 2nd, 2006 in Java.
This part of the article will concentrate on more complex object-relational mappings. Java Persistence has mappings for JDBC BLOBS and CLOBS, serializable objects and embeddable objects. The following will go through each of the mapping one by one.
1. The persistence provider will assume that every nontransient property in your entity bean class is persistent. If you do not want a property to be persistent, you can mark the field or the setter/getter methods with @javax.persistence.Transient annotation depending on the access type.
2. The @javax.persistence.Basic is the most primitive type of O/R mapping that covers all primitives and its wrapper types. The annotation has two interesting attributes: fetch() and optional().
The fetch() attribute defines that strategy for fetching data from the database, which takes two values: EAGER and LAZY. If the FetchType is LAZY, that particular property is not initialized until you actually access the field. The EAGER fetch strategy is the default and it is the opposite of LAZY.
The optional() attribute when set to true tells the persistence provider the property should be treated as nullable.
3. The @javax.persistence.Temporal annotation allows you to map java.util.Date or java.util.Calendar to a date, a time or a timestamp field in the database. The default type is timestamp.
4. The @javax.persistence.Lob annotation allows you to map large objects to java.sql.Blob and java.sql.Clob in the database. The persistence provider will choose whether to use Blob or Clob depends on the large object type:
Blobif the object type isbyte[],Byte[]orjava.io.SerializableClobif the object type ischar[],Char[]orjava.lang.String
5. The @javax.persistence.Enumerated maps Java enum types to the database. Both string representation and numeric ordinal number are supported.
6. The @javax.persistence.SecondaryTable allows you to map an entity bean to more than one table. To use the annotation, the primary key columns for the secondary table must be joinable with one or more columns in the primary table using the pkJoinColumns() attribute with embedded @javax.persistence.PrimaryKeyJoinColumn. Once the primary key columns are mapped, the rest of the columns will be mapped using @Column annotation.
You can use @javax.persistence.SecondaryTables if you have more than one secondary table.
7. The Java Persistence specification allows you to embed nonentity Java objects within your entity beans and map the properties of this embedded value object to columns within the entity’s table using @javax.persistence.Embedded. The rules for @Embedded is similar to @EmbeddedId in that you need to create an embeddable object and use @AttributeOverrides to embed.


2 Responses to “SCBCD Notes: Java Persistence (Part 4)”
Please Wait
Leave a Reply