JPA Annotation Otimo

download JPA Annotation Otimo

of 21

Transcript of JPA Annotation Otimo

  • 8/3/2019 JPA Annotation Otimo

    1/21

    Essential object-relational mappingin JPA

    10 Votes

    The Java Persistence API is the standard framework forpersistence in both Java SE and Java EE platforms. It is

    the outcome of the collaborative work of the industrysleading vendors in object-relational mapping, EJB andJDO, including a remarkable contribution from the open

    source community.

    In this post we summarize, one-by-one, the essential

    relationships between entities. We examine eachrelationship from the prespectives of Domain Model and

    Relation Model, and provide the associated construction

    in JPA. Finally, for each relationship we present anexample from the real world.

    The relationships are divided in two categories. For dailyuse presents the correct way to use common

    relationships in our application development activities. Onthe other hand, Containing pitfalls identify common

    mistakes.

    For daily use:

    1.One-to-One (one direction)2.One-to-One (both directions)3.Many-to-One (one direction)4.Many-to-One (both directions)5.One-to-Many (one direction)6.One-to-Many (both directions)

    http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-1http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-1http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-2http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-2http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-3http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-3http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-3http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-4http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-4http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-4http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-6http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-6http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-6http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-5http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-5http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-5http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-5http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-6http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-4http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-3http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-2http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-1http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/
  • 8/3/2019 JPA Annotation Otimo

    2/21

    7.Many-to-Many (both directions)Containing pitfalls:

    1.Many-to-Many (one direction)

    One-to-One (one direction)

    Domain model of an one-to-one unidirectional relationship

    @Entity

    class A {@Id int id;

    @OneToOneB b;

    }

    @Entityclass B {

    @Id int id;}

    Relation model of an one-to-one unidirectional relationship

    Example 1, One-to-One unidirectionalrelationship in JPA

    An employee has one desk.

    @Entity

    http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-5http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-7http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-7http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-8http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-8http://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://nikojava.files.wordpress.com/2011/08/image1b.pnghttp://nikojava.files.wordpress.com/2011/08/image1a.pnghttp://nikojava.files.wordpress.com/2011/08/image1b.pnghttp://nikojava.files.wordpress.com/2011/08/image1a.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-8http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-7
  • 8/3/2019 JPA Annotation Otimo

    3/21

    public class Employee implementsSerializable {

    @Idprivate int id;

    private String name;

    @OneToOneprivate Desk desk;

    }

    @Entitypublic class Desk implementsSerializable {

    @Id

    private int id;

    private String position;

    }

    Physical description of an one-to-one unidirectional relationship

    http://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://nikojava.files.wordpress.com/2011/08/image1d.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.html
  • 8/3/2019 JPA Annotation Otimo

    4/21

    ER diagram of an one-to-one unidirectional relationship

    One-to-One (both directions)

    Domain model of an one-to-one birectional relationship

    In this case we would like to keep a reference betweenboth sides.

    @Entityclass A {

    @Id int id;

    @OneToOneB b;}

    @Entityclass B {

    @Id int id;

    @OneToOne(mappedBy="b")A a;

    }

    Relation model of an one-to-one bidirectional relationship

    http://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://nikojava.files.wordpress.com/2011/08/image2b.pnghttp://nikojava.files.wordpress.com/2011/08/image2a.pnghttp://nikojava.files.wordpress.com/2011/08/image1c.pnghttp://nikojava.files.wordpress.com/2011/08/image2b.pnghttp://nikojava.files.wordpress.com/2011/08/image2a.pnghttp://nikojava.files.wordpress.com/2011/08/image1c.pnghttp://nikojava.files.wordpress.com/2011/08/image2b.pnghttp://nikojava.files.wordpress.com/2011/08/image2a.pnghttp://nikojava.files.wordpress.com/2011/08/image1c.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.html
  • 8/3/2019 JPA Annotation Otimo

    5/21

    Since the relation concerns both directions, we maychange the owning side.

    @Entity

    class A {@Id int id;

    @OneToOne(mappedBy="a")B b;

    }

    @Entityclass B {

    @Id int id;

    @OneToOneA a;

    }

    Relation model of an one-to-one bidirectional relationship

    Example 2, One-to-One bidirectionalrelationship in JPA

    A husband has exactly one wife. A wife has exactly onehusband.

    @Entitypublic class Husband implementsSerializable {

    @Id

    http://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://nikojava.files.wordpress.com/2011/08/image2c.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.html
  • 8/3/2019 JPA Annotation Otimo

    6/21

    private int id;

    private String name;

    @OneToOneprivate Wife wife;

    }

    @Entitypublic class Wife implementsSerializable {

    @Idprivate int id;

    private String name;

    @OneToOne(mappedBy="wife")private Husband husband;

    }

    We assume husband is the owning side, so he will holdthe foreign key.

    Physical description of an one-to-one bidirectional relationship

    http://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://nikojava.files.wordpress.com/2011/08/image2d.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToOne.html
  • 8/3/2019 JPA Annotation Otimo

    7/21

    ER diagram of a bidirection one-to-one relationship

    Now we can ask the wife Who is your husband?, as wellas, ask the husband Who is your wife?.

    Many-to-One (one direction)

    Domain model of a many-to-one unidirectional relationship

    @Entityclass A {

    @Id int id;

    @ManyToOne

    B b;}

    @Entityclass B {

    @Id int id;}

    Relation model of a many-to-one unidirectional relationship

    Example 3, Many-to-One unidirectional

    relationship in JPA

    http://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.htmlhttp://nikojava.files.wordpress.com/2011/08/image3b.pnghttp://nikojava.files.wordpress.com/2011/08/image3a.pnghttp://nikojava.files.wordpress.com/2011/08/image2e.pnghttp://nikojava.files.wordpress.com/2011/08/image3b.pnghttp://nikojava.files.wordpress.com/2011/08/image3a.pnghttp://nikojava.files.wordpress.com/2011/08/image2e.pnghttp://nikojava.files.wordpress.com/2011/08/image3b.pnghttp://nikojava.files.wordpress.com/2011/08/image3a.pnghttp://nikojava.files.wordpress.com/2011/08/image2e.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.html
  • 8/3/2019 JPA Annotation Otimo

    8/21

    There are millions of music fans out there. Each fan hashis favorite singer. Of course, a signer is not able to keep

    detailed records of his fans.

    @Entitypublic class Fan implementsSerializable {

    @Idprivate int id;

    private String name;

    @ManyToOneprivate Singer favoriteSinger;

    }

    @Entitypublic class Singer implementsSerializable {

    @Idprivate int id;

    private String name;

    }

    As a result, the foreign key goes to the fan.

    http://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.html
  • 8/3/2019 JPA Annotation Otimo

    9/21

    Physical description of a many-to-one unidirectional relationship

    ER diagram of a many-to-one unidirectional relationship

    Now we may ask any fan Who is your favorite signer?.

    Many-to-One (both directions)

    Domain model of a many-to-one bidirectional relationship

    @Entityclass A {

    @Id int id;

    @ManyToOne

    Bb;}

    @Entityclass B {

    @Id int id;

    @OneToMany(mappedBy="b")

    Collection listOfA;

    http://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.htmlhttp://nikojava.files.wordpress.com/2011/08/image5a.pnghttp://nikojava.files.wordpress.com/2011/08/image3d.pnghttp://nikojava.files.wordpress.com/2011/08/image3c.pnghttp://nikojava.files.wordpress.com/2011/08/image5a.pnghttp://nikojava.files.wordpress.com/2011/08/image3d.pnghttp://nikojava.files.wordpress.com/2011/08/image3c.pnghttp://nikojava.files.wordpress.com/2011/08/image5a.pnghttp://nikojava.files.wordpress.com/2011/08/image3d.pnghttp://nikojava.files.wordpress.com/2011/08/image3c.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.html
  • 8/3/2019 JPA Annotation Otimo

    10/21

    }

    Relation model of a many-to-one bidirectional relationship

    Example 4, Many-to-One bidirectionalrelationship in JPA

    The children of a father: A child knows his father and the

    father knows his children.

    @Entitypublic class Child implementsSerializable {

    @Idprivate int id;

    private String name;

    @ManyToOneprivate Father father;

    }

    @Entitypublic class Father implementsSerializable {

    @Idprivate int id;

    http://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.htmlhttp://nikojava.files.wordpress.com/2011/08/image3b1.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.html
  • 8/3/2019 JPA Annotation Otimo

    11/21

    private String surname;

    @OneToMany(mappedBy="father")

    private Collection children;

    }

    Physical description of a many-to-one bidirectional relationship

    ER diagram of a many-to-one bidirectional relationship

    One-to-Many (one direction)

    Domain model of a one-to-many unidirectional relationship

    It is the case when an entity has a set of characteristics.

    @Entityclass A {

    @Id int id;

    // A join table is assumed.@OneToManyCollection listOfB;

    http://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.htmlhttp://nikojava.files.wordpress.com/2011/08/image12.pnghttp://nikojava.files.wordpress.com/2011/08/image5c.pnghttp://nikojava.files.wordpress.com/2011/08/image5b.pnghttp://nikojava.files.wordpress.com/2011/08/image12.pnghttp://nikojava.files.wordpress.com/2011/08/image5c.pnghttp://nikojava.files.wordpress.com/2011/08/image5b.pnghttp://nikojava.files.wordpress.com/2011/08/image12.pnghttp://nikojava.files.wordpress.com/2011/08/image5c.pnghttp://nikojava.files.wordpress.com/2011/08/image5b.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.html
  • 8/3/2019 JPA Annotation Otimo

    12/21

    }

    @Entity

    class B {@Id int id;}

    Relation model of a one-to-many unidirectional relationship

    Thus, when declaring an OneToManyannotation without a mappedBy element, then a join

    table is assumed.

    Example 5, One-to-Many unidirectionalrelationship in JPA

    A library which provides facilities.

    @Entitypublic class Library implementsSerializable {

    @Idprivate int id;

    private String name;

    @OneToManyprivate Collection

    facilities;

    }

    http://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.htmlhttp://nikojava.files.wordpress.com/2011/08/image4b.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.html
  • 8/3/2019 JPA Annotation Otimo

    13/21

    @Entitypublic class Facility implements

    Serializable {

    @Idprivate String code;

    private String description;

    }

    Physical description of a one-to-many unidirectional relationship

    ER diagram of an one-to-many unidirectional relationship

    http://nikojava.files.wordpress.com/2011/08/image10.pnghttp://nikojava.files.wordpress.com/2011/08/image11.pnghttp://nikojava.files.wordpress.com/2011/08/image10.pnghttp://nikojava.files.wordpress.com/2011/08/image11.png
  • 8/3/2019 JPA Annotation Otimo

    14/21

    Now we can ask a library What facilities do youprovide?.The answer could be Scanning, Printing and

    Photocopying. This set of facilities may be provided by

    one or more libraries at the same time.

    If using a join table is not what you want, then youshould use the next relationship.

    One-to-Many (both directions)

    It is exactly the same with Many-to-One (bothdirections), looking via a mirror.

    Domain model of a one-to-many bidirectional relationship

    @Entityclass A {

    @Id int id;

    @OneToMany(mappedBy="a")Collection listOfB;

    }

    @Entityclass B {

    @Id int id;

    @ManyToOneA a;

    }

    http://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.htmlhttp://nikojava.files.wordpress.com/2011/08/image2c1.pnghttp://nikojava.files.wordpress.com/2011/08/image6a.pnghttp://nikojava.files.wordpress.com/2011/08/image2c1.pnghttp://nikojava.files.wordpress.com/2011/08/image6a.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.html
  • 8/3/2019 JPA Annotation Otimo

    15/21

    Relation model of an one-to-many bidirectional relationship

    Example 6, One-to-Many bidirectional

    relationship in JPAA manager who manages projects.

    @Entitypublic class Manager implementsSerializable {

    @Idprivate int id;

    private String name;

    @OneToMany(mappedBy="manager")private Collection

    projects;

    }

    @Entitypublic class Project implementsSerializable {

    @Idprivate int id;

    private String title;

    @ManyToOneprivate Manager manager;

    }

    http://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToOne.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/OneToMany.html
  • 8/3/2019 JPA Annotation Otimo

    16/21

    Physical description of a one-to-many bidirectional relationship

    ER diagram of an one-to-many bidirectional relationship

    Many-to-Many (both directions)

    Domain model of a many-to-many bidirectional relationship

    @Entityclass A {

    @Id int id;

    @ManyToManyCollection listOfB;

    }

    @Entityclass B {

    @Idint id;

    @ManyToMany(mappedBy="listOfB")Collection listOfA; }

    http://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.htmlhttp://nikojava.files.wordpress.com/2011/08/image7a.pnghttp://nikojava.files.wordpress.com/2011/08/image6c.pnghttp://nikojava.files.wordpress.com/2011/08/image6b.pnghttp://nikojava.files.wordpress.com/2011/08/image7a.pnghttp://nikojava.files.wordpress.com/2011/08/image6c.pnghttp://nikojava.files.wordpress.com/2011/08/image6b.pnghttp://nikojava.files.wordpress.com/2011/08/image7a.pnghttp://nikojava.files.wordpress.com/2011/08/image6c.pnghttp://nikojava.files.wordpress.com/2011/08/image6b.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.html
  • 8/3/2019 JPA Annotation Otimo

    17/21

    Of course, a join table is used.

    Relation model of a many-to-many bidirectional relationship

    Note: In a birectional many-to-many relationship weshould alwaysinclude the mappedBy element to either of

    the sides. This ensures that exactly one join table is used.

    Example 7, Many-to-Many bidirectionalrelationship in JPA

    An engineer works in many projects. A project occupiesmany engineers. This is a classic many-to-many

    relationship. Moreover it expands to both directions, as:

    An engineer should in which projects he is working. The project should know the engineers it occupies.@Entitypublic class Engineer implementsSerializable {

    @Idprivate int id;

    private String name;

    @ManyToMany@JoinTable(name="ENGINEER_PROJECT",

    joinColumns=@JoinColumn(name="ENGINEER_ID"),

    inverseJoinColumns=@JoinColumn(name="PROJECT_ID"))

    http://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.htmlhttp://nikojava.files.wordpress.com/2011/08/image7b.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.html
  • 8/3/2019 JPA Annotation Otimo

    18/21

    private Collectionprojects;

    }

    @Entitypublic class Project implementsSerializable {

    @Idprivate int id;

    private String title;

    @ManyToMany(mappedBy="projects")private Collection

    engineers;

    }

    Physical description of a many-to-many bidirectional relationship

    http://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.htmlhttp://nikojava.files.wordpress.com/2011/08/image7c.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.html
  • 8/3/2019 JPA Annotation Otimo

    19/21

    ER diagram of a many-to-many bidirectional relationship

    Pitfall: Many-to-Many (onedirection)

    Not specifying the mappedBy element in a many-to-manyrelationship, assumes two join tables and should be

    avoided.

    @Entityclass A {

    @Id int id;

    @ManyToManyCollection listOfB;

    }

    @Entityclass B {

    @Id int id;

    @ManyToMany

    Collection listOfA;}

    The same happens when the ManyToMany annotation isommitted on one side.

    @Entityclass A {

    @Id int id;

    http://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.htmlhttp://nikojava.files.wordpress.com/2011/08/image7d.pnghttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.htmlhttp://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.html
  • 8/3/2019 JPA Annotation Otimo

    20/21

    @ManyToManyCollection listOfB;

    }

    @Entityclass B {

    @Id int id;

    Collection listOfA;}

    Relation model of two many-to-many unidirectional relationship

    To avoid this pitfall, I would follow the instructionsofMany-to-Many (both directions)relationship.

    Notes

    1.The drawings were created usingOpenOffice.org Draw3.3.0

    2.The console output of the examples is fromMySQL5.5Command Line Client

    3.The EER diagrams of the examples were createdusingMySQL Workbench 5.2.34 CE, Revision 7780

    References

    http://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.htmlhttp://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-7http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-7http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-7http://www.openoffice.org/http://www.openoffice.org/http://www.mysql.com/http://www.mysql.com/http://www.mysql.com/http://wb.mysql.com/http://wb.mysql.com/http://nikojava.files.wordpress.com/2011/08/image8a.pnghttp://wb.mysql.com/http://www.mysql.com/http://www.openoffice.org/http://nikojava.wordpress.com/2011/08/04/essential-jpa-relationships/#jpa-relations-7http://download.oracle.com/javaee/6/api/javax/persistence/ManyToMany.html
  • 8/3/2019 JPA Annotation Otimo

    21/21

    M. Keith, M. Schincariol, Pro JPA 2: Mastering theJava Persistence API, Apress, United States of

    America, 2009.