JPA - Entity (Managed Classes)

1 - 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.

3 - Configuration

3.1 - Declaration

An entity can be defined using:

@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.

3.2 - Mapping

Mapping metadata specifies the mapping of the classes to the database and are in the form of:

4 - 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;
	}
}

5 - Eclipse Views

Details and Structure:

6 - Support

6.1 - 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;

7 - Support

7.1 - java.lang.NoClassDefFoundError: oracle/spatial/geometry/JGeometry

7.2 - 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

8 - Documentation / Reference

jpa/entity.txt ยท Last modified: 2017/09/06 09:16 by gerardnico