EJB3: Mapping Persistent Objects
by admin ~ October 21, 2008
Entities are real-world objects and can be expressed as nouns. Entities represent data in the database, so changes to an entity bean result in changes to the database. When a new entity is created and persisted, a record must me inserted into the database. When an entity is used and its state changes, that must be reflected to the database.
An entity must implement java.io.Serializable so that it can be used as a DTO.
There are two ways to implement entity beans. One way is to start from Java Object Model and produce database schema out of it. Second way is that, if you have already the database schema and so Java Object Model can be produced out of it.
@javax.persistence.Table annotation is used to tell the EntityManager that which database table, entity maps to. If you do not specify this annotation, the default table name is the unqualified class name of the entity bean.
@javax.persistence.Column annotation is used to tell the EntityManager that a particular attribute is mapped to which column of the database table. If you do not specify this annotation, the default column name is the name of the attribute.
A primary key is the identity of the entity bean. A primary key is always unique. Sometimes, a single property of the entity is not sufficient to uniquely identify the entity. So primary keys can map to one or more properties of the entity.
@javax.persistence.Id annotation identifies one or more properties that make up the primary for your table. The primary key can be either tell to be generated by the persistence provider or create it manually. For the first option @javax.persistence.GeneratedValue annotation can be used. You can tell the strategy to be used for generating primary key e.g. GeneratedType.AUTO is the most commonly used one. For the second option you have to use Table Generators. This strategy designates a user-defined relational table from which the numeric keys will be generated. Some RDBMs, specifically Oracle has built-in structure to generate IDs sequentially. This is the SEQUENCE generator strategy.
For Primary-Key Classes and Composite Keys @javax.persistence.IdClass and @javax.persistence.EmbededId annotations can be used.
@IdClass: This is a class level annotation. This specifies that which primary-key class should be used when interacting with the entity manager. This class must fulfill the following requirements:
- It must me serializable.
- It must have a public no-arg constructor
- It must implement the equals() and hashCode() methods
The entity class should have the same exact properties as the primary-key class and these properties must be annotated with multiple @Id annotations.
@javax.persistence.EmbeddedId and @javax.persistence.Embaddable annotations can be used if you want to embed your primary-key class directly in your entity bean class. If you don’t want to use the @Column mappings with the primary-key class, or you just want to override them, you can use @AttributOverrides to declare them directly in your bean class.
By default the persistence manager will assume that every nontransient property in your class are persistent even if the property is not having any mapping metadata associated with it. So if you want some property to be transient, then associate it with @Transient metadata.
@Basic and FetchType: This is the default mapping type for properties which are primitives and primitive wrapper types. Usually one is never needed to annotate with this annotation. You will need to specify the FetchType to tell whether a particular property is loaded lazily or eagerly when the persistent object is first fetched from the database. If this attribute is set to LAZY, it means, that particular property will not be initialized until you actually access this field. optional attribute is useful when database schema is being generated by the persistence provider. If this attribute is set to true, the property is treated as nullable.
The @Temporal annotation provides additional information to the persistence provider about the mapping of a java.util.Date or java.util.Calendar property. This annotation allows you to map these object types to a date, a time, a timestamp field in the database.
The @javax.persistence.Lob annotation is used to map large objects like java.sql.Blob (binary data), java.sql.Clob (character data). Properties annotated with a @Lob are persisted in a Blob if the Java type is byte[], Byte[], or java.io.Serializable and in a Clob if the Java type is char[], Character[], or java.lang.String.
The @Enumerated annotation maps Java enum types to the database. A Java enum property can be mapped either to the string representation or to the numeric ordinal number of the enum value. If this annotation is not used to map a property, the ORDINAL EnumType value is assumed.
@javax.persistence.SecondaryTable annotation can be used to map an entity bean class to one or more tables.
@Embedded Objects: You can embed nonentity Java objects within your enity beans and map the properties of this embeddaed value object to columns within the entity’s table. These objects do not have thier identity and they are owned by the entity bean class.
References:
“Java Persistence with Hibernate” by Christian Bauer and Gavin King.
“Enterprise JavaBeans 3.0″ by Bill Burke & Richard Monson-Haefel
JBoss Documentations for EJB 3



Leave a Reply