SCBCD Notes: Java Persistence (Part 3)
Published December 1st, 2006 in Java.In this part of the article, we concentrate on creating entity bean.
Java Persistence requires an entity bean to have two pieces of metadata: @javax.persistence.Entity to tell the EJB container to map the class to the database table and @javax.persistence.Id, the primary key of the table. By default, the persistence provider assumes name-to-name and type-to-type mapping of the fields to database columns. The table name is also the same as the unqualified name of the entity bean class unless specified by the name() attribute.
There are two ways of applying @Id annotation: Java Bean style - mark on setter and getter methods or on each field. In either case, the provider will assume the following methods or fields are also persistent properties. As usual, XML mapping is also available if you prefer.
If you want the persistence provider to auto generate the primary key, you need to add @javax.persistence.GeneratedValue in addition to @Id. The strategy() attribute governs the primary key generation. The valid values are as follow:
AUTO: Pick the most appropriate strategy.IDENTITY: Use the database identity column.SEQUENCE: Use a generator defined using@SequenceGenerator.TABLE: Use a generator defined using@TableGenerator.
To define composite keys, you can either use @javax.persistence.IdClass annotation or @javax.persistence.EmbeddedId annotation.
In the case of @IdClass, the entity bean class does not use the class internally. In your bean class, you designate one or more properties that make up your primary key, using the @Id annotation. These properties must map exactly to properties in the @IdClass by providing the class object as its value.
The primary key class must implement java.io.Serializable, has a public no-arg constructor and implements equals() and hashCode() methods.
In the @EmbeddedId case, the primary key class is used directly in the bean class. To complicate things further, there are variations on how to embed the primary key class. You can either embed an Embeddable primary key class defined outside the bean class or use @AttributeOverrides annotation to declare them directly in your bean class.

To map to an existing database table using a vendor tool, you can use @javax.persistence.Table. The annotation has four attributes:
name(): Overwrite default table name.catalog(): Identify the relational catalog the table belongs to.schema(): Identify the relational schema the table belongs to.uniqueConstraints(): Specify unique column constraints to be included in the generated DDL. This allows you to add vendor specific features.
Similarly, @javax.persistence.Column can be used to define object model for the vendor mapping tool to create database schema. There are ten attributes in total:
name(): Specify the column name, the default is the field name.table(): Used for multi-table mappings.unique(): Specify whether the column is a unique key.nullable(): Specify whether the column can beNULL.insertable(): Specify whether the column is included in the SQLINSERTDML generated by the provider.updatable(): Specify whether the column is included in the SQLUPDATEDML generated by the provider.columnDefinition(): Allow you to define the exact DDL used to define the column type.length(): Specify the length of theVARCHARwhen you have aStringfield.precision(): Specify the precision of the decimal column.scale(): Specify the scale of the decimal column.


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