Relations in Java Persistence API
Relationships are persistent fields in persistable classes that reference other entity objects
- One to One
- One to Many
- Many to One
- Many to Many
public Class Customer {
private int id;
private CustomerRecord customerRecord;
}
public Class CustomerRecord {
private int id;
}- Customer owns CustomerRecord
- CustomerRecord is owned by Customer
Annotation Elements
CascadeType[] cascade
(Optional) The operations that must be cascaded to the target of the association.
By default no operations are cascaded.
Default value: {}
FetchType[] fetch
(Optional) Whether the association should be lazily loaded or must be eagerly fetched. The EAGER strategy is a requirement on the persistence provider runtime that the associated entity must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime.
Default value: javax.persistence.FetchType.EAGER
String mappedBy
Optional) The field that owns the relationship. This element is only specified on the inverse (non-owning) side of the association.
Default value: ""
boolean optional
(Optional) Whether the association is optional. If set to false then a non-null relationship must always exist.
Default value: true
boolean orphanRemoval
(Optional) Whether to apply the remove operation to entities that have been removed from the relationship and to cascade the remove operation to those entities.
Default value: false
Class targetEntity
(Optional) The entity class that is the target of the association.
Defaults to the type of the field or property that stores the association.
Default value: void.class
@OneToOne
Defines a single-valued association to another entity that has one-to-one multiplicity.
- cascade
- fetch
- mappedBy
- optional
- orphanRemoval
- targetEntity
One-to-one association that maps a foreign key column
Customer
ID
...
CUSTREC_ID
CustomerRecord
ID
...
One-to-one association that maps a foreign key column
// On Customer class:
@OneToOne(optional=false)
@JoinColumn(
name="CUSTREC_ID", unique=true, nullable=false, updatable=false)
public CustomerRecord getCustomerRecord() { return customerRecord; }
// On CustomerRecord class:
@OneToOne(optional=false, mappedBy="customerRecord")
public Customer getCustomer() { return customer; }One-to-one association that assumes both the source and target share the same primary key values.
Employee
ID
...
EmployeeInfo
ID
...
One-to-one association that assumes both the source and target share the same primary key values.
// On Employee class:
@Entity
public class Employee {
@Id Integer id;
@OneToOne @MapsId
EmployeeInfo info;
...
}
// On EmployeeInfo class:
@Entity
public class EmployeeInfo {
@Id Integer id;
...
}Embeddable Classes in Entities ?
Address
id
street1
city
province
zip
plusFour
country
...
Embeddable Classes in Entities ?
@Embeddable
public class ZipCode {
String zip;
String plusFour;
...
}
This embeddable class is used by the Address entity:
@Entity
public class Address {
@Id
protected long id
String street1;
String street2;
String city;
String province;
@Embedded
ZipCode zipCode;
String country;
...
}One-to-one association from an embeddable class to another entity.
Employee
id
officeNumber
parkingSpotId
...
ParkingSpot
id
garage
...
One-to-one association from an embeddable class to another entity.
@Entity
public class Employee {
@Id int id;
@Embedded LocationDetails location;
...
}
@Embeddable
public class LocationDetails {
int officeNumber;
@OneToOne ParkingSpot parkingSpot;
...
}
@Entity
public class ParkingSpot {
@Id int id;
String garage;
@OneToOne(mappedBy="location.parkingSpot") Employee assignedTo;
...
}@OneToMany
Defines a many-valued association with one-to-many multiplicity.
- cascade
- fetch
- mappedBy
- orphanRemoval
- targetEntity
One-to-Many association using generics
Customer
id
...
Order
id
CUST_ID
...
One-to-Many association using generics
// In Customer class:
@OneToMany(cascade=ALL, mappedBy="customer")
public Set<Order> getOrders() { return orders; }
In Order class:
@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
public Customer getCustomer() { return customer; }One-to-Many association without using generics
// In Customer class:
@OneToMany(targetEntity=com.acme.Order.class, cascade=ALL,
mappedBy="customer")
public Set getOrders() { return orders; }
// In Order class:
@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
public Customer getCustomer() { return customer; }@ManyToOne
Defines a single-valued association to another entity class that has many-to-one multiplicity.
- cascade
- fetch
- optional
- targetEntity
@ManyToOne(optional=false)
@JoinColumn(name="CUST_ID", nullable=false, updatable=false)
public Customer getCustomer() { return customer; }Employee
id
jobDescription
pmId
...
ProgramManger
id
...
@Entity
public class Employee {
@Id int id;
@Embedded JobInfo jobInfo;
...
}
@Embeddable
public class JobInfo {
String jobDescription;
@ManyToOne ProgramManager pm; // Bidirectional
}
@Entity
public class ProgramManager {
@Id int id;
@OneToMany(mappedBy="jobInfo.pm")
Collection<Employee> manages;
}@ManyToMany
Defines a many-valued association with many-to-many multiplicity.
- cascade
- fetch
- mappedBy
- targetEntity
Customer
id
...
Phone
id
...
CUST_PHONES
cid
pid
// In Customer class:
@ManyToMany
@JoinTable(name="CUST_PHONES")
public Set<PhoneNumber> getPhones() { return phones; }
// In PhoneNumber class:
@ManyToMany(mappedBy="phones")
public Set<Customer> getCustomers() { return customers; } // In Customer class:
@ManyToMany(targetEntity=com.acme.PhoneNumber.class)
public Set getPhones() { return phones; }
// In PhoneNumber class:
@ManyToMany(targetEntity=com.acme.Customer.class, mappedBy="phones")
public Set getCustomers() { return customers; }// In Customer class:
@ManyToMany
@JoinTable(name="CUST_PHONE",
joinColumns=
@JoinColumn(name="CUST_ID", referencedColumnName="ID"),
inverseJoinColumns=
@JoinColumn(name="PHONE_ID", referencedColumnName="ID")
)
public Set<PhoneNumber> getPhones() { return phones; }
// In PhoneNumberClass:
@ManyToMany(mappedBy="phones")
public Set<Customer> getCustomers() { return customers; }Thank you !
JPA relations
By Popdan Daniel
JPA relations
Relations in Java Persistence API
- 666