JPA - Entity (Managed Classes)
Table of Contents
About
In a Object-Relational mapping context, an entity represents a table in a relational database, and each entity instance corresponds to a row in the table.
An entity is a JPA managed class.
Entity are enterprise beans (EJB3) that contain persistent data and that can be saved in various persistent data stores. The entity beans represent data from a database; each entity bean carries its own identity.
An entity is just a standard JavaBean with private properties and public getters and setters.
To manage entities in your code, you need to obtain first an JPA entity manager.
Articles Related
Configuration
Declaration
An entity can be defined using:
- the orm.xml file and the <entity> tag.
- or the @Entity annotation
@Entity
public class className implements Serializable {
...
}
The entity class must also be listed in the persistence.xml file, unless you set the tag <exclude-unlisted-classes> to false.
Mapping
Mapping metadata specifies the mapping of the classes to the database and are in the form of:
- metadata annotations (@Entity, @Table, @ManyToOne and few other annotations).
- and/or XML metadata (ORM.xml)
IDE Entity Generation
When you generate entities from a database with an IDE such as Eclipse OEPE, the generated source code is annotated with JPA annotations that designate which type a given property is.
Object-Relational (OR) Mappings can be generated through two different mechanisms:
- Generate Entities from Table: Reverse Engineering the Schema to create object relational mappings.
- Generate Entity from Java Class: Generating (Mappings|JPA entities) from an (Object Model|POJO) classes (by providing annotations) i.e. a Top-Down development scenario.
Example of POJO from where you can generate an entity.
package com.oracle.ticketsystem.beans;
import java.io.Serializable;
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
// Columns of the department table
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The class after field access entity generation is annotated.
package com.oracle.ticketsystem.beans;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.Basic;
@Entity()
@Table(name = "DEPARTMENT", schema = "APP")
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
// Columns of the department table
@Id()
@Column(name = "ID", unique = true, nullable = false, updatable=false)
private long id;
@Basic()
@Column(name = "NAME", nullable = false, length = 100)
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Eclipse Views
Support
Multiple writable mappings exist for the field
Exception [EclipseLink-48] (Eclipse Persistence Services - 2.3.2.v20111125-r10461):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Multiple writable mappings exist for the field [TECHNICIAN.DEPTID].
Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.ManyToOneMapping[department]
Descriptor: RelationalDescriptor(com.oracle.ticketsystem.beans.Technician --> [DatabaseTable(TECHNICIAN)])
The problem is that the column TECHNICIAN.DEPTID is mapped two times:
- with the column
- with the relationship
Only one mapping can be writable and this is normally with the column mapping.
Before in the technician entity:
@ManyToOne
@JoinColumn(name="deptid")
private Department department;
After with Resolution:
@ManyToOne
@JoinColumn(name="deptid", insertable=false, updatable=false)
private Department department;
Support
java.lang.NoClassDefFoundError: oracle/spatial/geometry/JGeometry
Oracle XDB
To support Oracle XDB:
Librairy | From | To |
---|---|---|
xdb.jar | <ORACLE_DATABASE_HOME>/rdbms/jlib | <WEBLOGIC_HOME>/server/lib |
xml.jar | <ORACLE_DATABASE_HOME>/lib | <WEBLOGIC_HOME>/server/lib |
xmlparserv2.jar | <ORACLE_DATABASE_HOME>/lib | <WEBLOGIC_HOME>/server/lib |
Documentation / Reference
- Introduction to EclipseLink JPA (ELUG) for more information on the annotation.