I have a problem modeling / understanding partial , disjoint inheritance with JPA annotations.
Here are four tables:
CREATE TABLE Persons (
id INTEGER NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
PRIMARY KEY (id));
CREATE TABLE Referees (
id INTEGER NOT NULL,
license_nbr VARCHAR(10) DEFAULT NULL NULL,
PRIMARY KEY (id),
CONSTRAINT referees_persons_fk
FOREIGN KEY (id)
REFERENCES Persons (id)
ON DELETE CASCADE
ON UPDATE CASCADE);
CREATE TABLE Coaches (
id INTEGER NOT NULL,
license_nbr VARCHAR(10) DEFAULT NULL NULL,
PRIMARY KEY (id),
CONSTRAINT coaches_persons_fk
FOREIGN KEY (id)
REFERENCES Persons (id)
ON DELETE CASCADE
ON UPDATE CASCADE);
CREATE TABLE Players (
id INTEGER NOT NULL,
registration_nbr VARCHAR(20) DEFAULT NULL NULL,
PRIMARY KEY (id),
CONSTRAINT players_persons_fk
FOREIGN KEY (id)
REFERENCES Persons (id)
ON DELETE CASCADE
ON UPDATE CASCADE);
My abbreviated entity classes:
@Entity
@Table(name = "Persons")
@Inheritance(strategy = InheritanceType.JOINED)
public class Person implements Serializable
{
@Id
@Column(name = "id")
private Integer id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
...
}
@Entity
@Table(name = "Referees")
public class Referee extends Person implements Serializable
{
@Column(name = "license_nbr")
private String licenseNbr = null;
...
}
@Entity
@Table(name = "Coaches")
public class Coach extends Person implements Serializable
{
@Column(name = "license_nbr")
private String licenseNbr = null;
...
}
@Entity
@Table(name = "Players")
public class Player extends Person implements Serializable
{
@Column(name = "registration_nbr")
private String registrationNbr = null;
...
}
Partial Inheritance: Individuals may exist without appropriate referees, coaches or players
Disjoint inheritance: there can be any number of relevant entities, that is, a person can simultaneously be a judge, coach and player, possibly any combination of the three. Each subcategory can have only one or one object, but not several.
, , Person . , Person/s , .
Java ORM? ?
, , :
Person pe1 = em.find(Person.class, 1);
System.out.println("Loaded person = " + pe1);
Player pl1 = em.find(Player.class, 1);
System.out.println("Loaded player = " + pl1);
Referee re1 = em.find(Referee.class, 1);
System.out.println("Loaded referee = " + re1);
Coach ch1 = em.find(Coach.class, 1);
System.out.println("Loaded coach = " + ch1);
System.out.println();
System.out.println();
Person pe2 = em.find(Person.class, 2);
System.out.println("Loaded person = " + pe2);
Player pl2 = em.find(Player.class, 2);
System.out.println("Loaded player = " + pl2);
Referee re2 = em.find(Referee.class, 2);
System.out.println("Loaded referee = " + re2);
Coach ch2 = em.find(Coach.class, 2);
System.out.println("Loaded coach = " + ch2);
System.out.println();
System.out.println();
Person pe3 = em.find(Person.class, 3);
System.out.println("Loaded person = " + pe3);
Player pl3 = em.find(Player.class, 3);
System.out.println("Loaded player = " + pl3);
Referee re3 = em.find(Referee.class, 3);
System.out.println("Loaded referee = " + re3);
Coach ch3 = em.find(Coach.class, 3);
System.out.println("Loaded coach = " + ch3);
EclipseLink 2.1.1 2.2.0 EntityManager.find(...), Hibernate, , . " " . , ORM JPA, .
, Hibernate Player, Coach Referee, Player.
JavaSE/HSQLDB/Hibernate :
http://www.kawoolutions.com/media/persons-partial-nondisjoint-inheritance.zip
, JPA ? ( !)?