IS907 Java EE JPA Mapping relationships Mapping of

  • Slides: 9
Download presentation
IS-907 Java EE JPA: Mapping relationships

IS-907 Java EE JPA: Mapping relationships

Mapping of pointer fields • JPA will automatically map fields of primitive types (int,

Mapping of pointer fields • JPA will automatically map fields of primitive types (int, float, boolean etc. ) and String to table columns • Fields of class types (pointers to other objects) are different: • The class maps to a table, so the pointer represents a relation • We must specify what kind of relation Even Åby Larsen (even. larsen@uia. no) IS-102 Introduksjon 2

JPA and relationships • • many-to-one relationships are nearly always represented in ER-models as

JPA and relationships • • many-to-one relationships are nearly always represented in ER-models as a foreign key column on the “many” side that references the primary key on the “one” side. Department - depno 1 * Employee - depno In java the relationship may be modelled • as a Department attribute in the Employee class, or • as a list of Employee in the Department class, or • both • We have to consider directionality IS-202 - Course Info 3

Unidirectional One-to-One Relationships @Entity public class Employee {. . . @One. To. One private

Unidirectional One-to-One Relationships @Entity public class Employee {. . . @One. To. One private Parking. Space parking. Space; } • creates a foreign key with a unique constraint in the employee table Even Åby Larsen (even. larsen@uia. no) IS-102 Introduksjon 4

Bidirectional One-to-One Relationships @Entity public class Employee {. . . @One. To. One private

Bidirectional One-to-One Relationships @Entity public class Employee {. . . @One. To. One private Parking. Space parking. Space; } @Entity public class Parking. Space { @One. To. One(mapped. By=“parking. Space”) private Employee emp, } • creates a foreign key with a unique constraint in the employee table • uses the same foreign key for the reverse pointer Even Åby Larsen (even. larsen@uia. no) IS-102 Introduksjon 5

Unidirectional Many-to-One Relationship @Entity public class Employee {. . . @Many. To. One private

Unidirectional Many-to-One Relationship @Entity public class Employee {. . . @Many. To. One private Department department; } • creates a foreign key column for department in the employee table Even Åby Larsen (even. larsen@uia. no) IS-102 Introduksjon 6

Bidirectional Many-to-One Relationship @Entity public class Employee {. . . @Many. To. One private

Bidirectional Many-to-One Relationship @Entity public class Employee {. . . @Many. To. One private Department department; } @Entity public class Department {. . . @One. To. Many(mapped. By=“department”) List<Employee> employees; } • creates a foreign key column for department in the employee table • the foreign key is reused to populate the list in department Even Åby Larsen (even. larsen@uia. no) IS-102 Introduksjon 7

Unidirectional One-to-Many Relationships @Entity public class Department {. . . @One. To. Many List<Employee>

Unidirectional One-to-Many Relationships @Entity public class Department {. . . @One. To. Many List<Employee> employees; } • There is no department field in Employee, and no mapped. By element • Nowhere to put the foreign key • A join table will be generated Even Åby Larsen (even. larsen@uia. no) IS-102 Introduksjon 8

Many-to-Many Relationships @Entity public class Employee {. . . @Many. To. Many private List<Project>

Many-to-Many Relationships @Entity public class Employee {. . . @Many. To. Many private List<Project> projects; } @Entity public class Project{. . . @Many. To. Many(mapped. By=“projects”) private List<Employee> employees; } • generates a join table • employees can be omitted from Project, making the relationship unidirectional Even Åby Larsen (even. larsen@uia. no) IS-102 Introduksjon 9