Thursday, September 1, 2011

JPA2

82

Identifier Generation

@GeneratedValue

AUTO, TABLE, SEQUENCE, or IDENTITY

@Entity
public class Employee {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
// ...
}

Id Generation Using a Table

@Id @GeneratedValue(strategy=GenerationType.TABLE)
private int id;

@TableGenerator(name="Emp_Gen")
@Id @GeneratedValue(generator="Emp_Gen")
private int id;

@TableGenerator(name="Emp_Gen",
table="ID_GEN",
pkColumnName="GEN_NAME",
valueColumnName="GEN_VAL")

@TableGenerator(name="Address_Gen",
table="ID_GEN",
pkColumnName="GEN_NAME",
valueColumnName="GEN_VAL",
pkColumnValue="Addr_Gen",
initialValue=10000,
allocationSize=100)
@Id @GeneratedValue(generator="Address_Gen")
private int id;

Id Generation Using a Database Sequence

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
private int id;

@SequenceGenerator(name="Emp_Gen", sequenceName="Emp_Seq")
@Id @GeneratedValue(generator="Emp_Gen")
private int getId;

CREATE SEQUENCE Emp_Seq
MINVALUE 1
START WITH 1
INCREMENT BY 50

Id Generation Using Database Identity

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

There is no generator annotation for IDENTITY because it must be defined as part of the database schema definition for the primary key column of the entity. Because each entity primary key column defines its own identity characteristic, IDENTITY generation cannot be shared across multiple entity types.
Another difference, hinted at earlier, between using IDENTITY and other id generation strategies is that the identifier will not be accessible until after the insert has occurred

Relationships

Relantionship Concepts:

  • Roles

  • Directionality

  • Cardinality

  • Ordinality




Single-Valued Associations/
An association from an entity instance to another entity instance (where the cardinality of the target is “one”) is called a single-valued association. The many-to-one and one-to-one relationship mappings fall into this category because the source entity refers to at most one target entity.

Many-to-One Mappings
@Entity
public class Employee {
// ...
@ManyToOne
private Department department;
// ...
}

A many-to-one mapping is defined by annotating the attribute in the source entity (the attribute that refers to the target entity) with the @ManyToOne annotation


Using Join Columns
In almost every relationship, independent of source and target sides, one of the two sides will have the join column in its table. That side is called the owning side or the owner of the relationship. The side that does not have the join column is called the non-owning or inverse side.

@Entity
public class Employee {
@Id private int id;
@ManyToOne
@JoinColumn(name="DEPT_ID")
private Department department;
// ...
}

One-to-One Mappings

@Entity
public class Employee {
@Id private int id;
private String name;
@OneToOne
@JoinColumn(name="PSPACE_ID")
private ParkingSpace parkingSpace;
// ...
}

Bidirectional One-to-One Mappings

@Entity
public class ParkingSpace {
@Id private int id;
private int lot;
private String location;
@OneToOne(mappedBy="parkingSpace")
private Employee employee;
// ...
}

The value of mappedBy is the name of
the attribute in the owning entity that points back to the inverse entity.
The two rules, then, for bidirectional one-to-one associations are the following:
• The @JoinColumn annotation goes on the mapping of the entity that is mapped to
the table containing the join column, or the owner of the relationship. This might
be on either side of the association.
• The mappedBy element should be specified in the @OneToOne annotation in the
entity that does not define a join column, or the inverse side of the relationship.



95

No comments: