Cmobilecom JPA 2.0.1 Developer Guide

8. Entity Identifier

Entity identifier can be a simple id, embedded id, IdClass or derived. Simple integral identifiers can be generated by an id generator.

Id Generators

If an entity type has a single id attribute whose type is an integral number (e.g., BigInteger), entity identifiers can be generated using one of the strategies: AUTO, IDENTITY, SEQUENCE and TABLE.

Auto Generator

Auto generators use identity or sequence to generator id values depending on underlying database features. For example, MySql supports identity (auto increment), SQL server supports identity and sequence, and oracle supports sequence.

Annotations:

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;
XML:
	<id name="id">
		<generated-value strategy="AUTO"/>
	</id>

Identity Generator

Identity generators use identity column to generate id values, which supports auto increment after each insertion DML. For example,

Annotations:

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
XML:
	<id name="id">
		<generated-value strategy="IDENTITY"/>
	</id>

Sequence Generator

Identity generators use database sequence to generate id values. For example,

Annotations:

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGen")
	@SequenceGenerator(name = "sequenceGen", sequenceName = "Id_Gen_Sequence",
		initialValue = 100, allocationSize = 30)
XML:
	<id name="id">
		<generated-value strategy="SEQUENCE" generator="sequenceGen"/>
		<sequence-generator name="sequenceGen" sequence-name="Id_Gen_Sequence"
			initial-value="100" allocation-size="30"/>
	</id>

Table Generator

Table generators use table to keep track of generated id values. To generate id value for an entity, increase current column value as a new id value. e.g.,

Annotations:

	@Id
	@GeneratedValue(strategy=GenerationType.TABLE, generator="tableGen")
	@TableGenerator(name="tableGen", table="Id_Gen_Table",
		pkColumnName="entityType", valueColumnName="maxId",
		pkColumnValue="Employee", initialValue=1, allocationSize=1)
XML:
	<id name="id">
		<generated-value strategy="TABLE" generator="tableGen"/>
		<table-generator name="tableGen" table="Id_Gen_Table"
						 pk-column-name="entityType" value-column-name="maxId"
						 pk-column-value="Employee" initial-value="1" allocation-size="1" />
	</id>

Derived Identifier

The identifier of a dependent entity can be derived from its parent entity. For example,
@Entity
public class Employer {
	@Id
	private Long id;
}

@Embeddable
public class DepartmentId {
	private String name;
	private Long employer;
	
	// getter/setter, equals(), hashCode()
}

@Entity
public class Department {
	@EmbeddedId
	private DepartmentId id;

	@MapsId("employer")
	@ManyToOne
	private Employer employer;
	
	public Department() {
	
	}
	
	public Department(String name, Employer employer) {
		this.id = new DepartmentId();
		this.id.setName(name);
		this.id.setEmployer(employer.getId());
	
		this.employer = employer;
	}
}
The Department entity identifier is DepartmentId(EmbeddedId). The "employer" relation attribute of Department is mapped to the "employer" field of the DepartmentId. So there will not be a separated database column for the relation attribute "employer". Note that the type of "employer" field is the identifier type of parent entity "Employer".

When setting the identifier of a dependent entity, also set corresponding MapsId relation attribute (if any) value to its parent entity as the example constructor Department(String name, Employer employer) above. They must be consistent.

The following code is IdClass version.

@Entity
public class Employer {
	@Id
	private Long id;
}
	
public class DepartmentId {
	private String name;
	private Long employer;
	
	// getter/setter, equals(), hashCode()
}

@Entity
@IdClass(DepartmentId.class)
public class Department {
	@Id
	private String name;

	@Id
	@ManyToOne
	private Employer employer;
}
O/R MappingJPQLFrames / No Frames