View RSS Feed

My Java Tips

Java Persistence API - annotations

Rate this Entry
by , 10-31-2011 at 06:15 PM (778 Views)
The Java Persistence API heavily depends on the metadata annotations. The API consists of:

Java Persistence API
The Java Persistence query language
Metadata annotations


An entity is a persistence object. It is coded as a POJO, and marked as an entity with the @Entity (javax.persistence.Entity) annotation. By default, all properties/fields are persisted into the datastore, except those marked with the @Transient annotation. Following example shows few annotations that are used with entity beans.

Java Code:
package university;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Id;

@Entity
@Table (name="Dept")
public class Department implements Serializable {
@Id
@Column(name="id", nullable=false)
private String deptId;
@Column(name="name")
private String deptName;
private String location;

public void setDeptId(String deptId) {
this.deptId = deptId;
}

public String getDeptId() {
return this.deptId;
}

public void setDeptName(String deptName) {
this.deptName = deptName;
}

public String getDeptName() {
return this.deptName;
}

public void setLocation(String location) {
this.location = location;
}

public String getLocation() {
return location;
}
..................
..................
}
We used @Entity to mark a simple POJO-based class as an entity. The table represented by the entity is denoted using the @Table annotation. Remember, if the entity name is the same as that of the table name, @Table is not required. The @Column annotation specifies the database column that corresponds to the property/field name and if a field is not annotated with @Column, the column name is assumed to be the same as that of the field name in the persistence storage. @Id annotation is used to make a field as primary key for the entity class.

The relationship multiplicities among entities may exist and there are annotations for that as well.


If each entity instance is related to a single instance of another entity, then one-to-one relation exists and

@OneToOne(javax.persistence.OneToOne) is used.
If an entity instance can be related to multiple instances of other entities, then one-to-many relation exists and

@OneToMany(javax.persistence.OneToMany) is used.
If multiple instances of an entity can be related to a single instance of the other entity,then many-to-one relation exist and

@ManyToOne(javax.persistence.ManyToOne) is used.

If entity instances can be related to multiple instances of each other, then we say that many-to-many relation exist and @ManyToMany (javax.persistence.ManyToMany) is used.

Submit "Java Persistence API - annotations" to Facebook Submit "Java Persistence API - annotations" to Digg Submit "Java Persistence API - annotations" to del.icio.us Submit "Java Persistence API - annotations" to StumbleUpon Submit "Java Persistence API - annotations" to Google

Tags: None Add / Edit Tags
Categories
Annotations

Comments