IntelliJ IDEA 13.1 Web Help

Some features described here are available in Ultimate edition only.

When relationships are created between entities:

  • The respective annotated properties are added to the source files of the entity classes, marked with gutter icons in the editor:

    jpaNewRelationship.png

  • Links appear in the ER diagram of a persistence unit:

    jpaNewRelationshipDiagram.png

  • Nodes are added to the entity view in the Persistence tool window:

    jpaNewRelationshipProjectView.png

IntelliJ IDEA suggests several ways to create relationships between entities:

To create a relationship using editor
  1. Open the desired entity class for editing.
  2. Type field declaration for a new relationship. For example,
                private Book bookTitle;
                
  3. Create getter and setter methods. To do that, press Alt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertAlt+InsertCtrl+N or Alt+InsertAlt+InsertAlt+InsertCommand N or Control EnterControl N or Control EnterCommand N or Control Enter, and select Getter and Setter from the suggestion list. For example:
                public Book getBookTitle() {
        return 
                bookTitle;
        }
    public void setBookTitle(Book bookTitle){
        this.bookTitle = bookTitle;
        }
                
  4. Annotate the field with the desired relationship annotation. For example:
                @OneToOne
                

    If the required import is missing, use the suggested quick fix.

  5. If required, next to the annotation, specify the following parameters:
    • Strategy for fetching data from the database
    • Cascadable options to be propagated to the associated entities
    • Owning entity
    • Target entity
    • Mapping field

    For example:

                @OneToOne(cascade = CascadeType.ALL,
        fetch = FetchType.LAZY, optional = false,
        mappedBy = "takenBy")
                    

    Note

    • For creating bidirectional relationships, you have to edit both the owner and the target entity classes.
    • Coding assistance is available.
To create relationships using drag-and-drop in er diagram
  1. In the Persistence tool window, right-click the desired persistence unit, and select ER Diagram in the context menu.
  2. In the diagram, select the source entity and draw a link to the target entity.
  3. In the Create Relationship dialog box, specify the options for the owner and the inverse side of a relationship. Note that if you specify options for one side of a relationship only, it will be created unidirectional. In particular, specify the following options:
    • Relationship name
    • Multiplicity
    • Owner of a relationship
    • Cascading options
    • Fetching strategy

    Click OK.

To create a relationship using an entity context menu
  1. In the Persistence tool window, right-click the desired persistence unit and choose New | <relationship type> on the context menu.

    Tip

    You can invoke the same command on the context menu of an entity in the ER diagram.

  2. In the dialog box that opens, specify the relationship name and type. By default, the entity in question becomes the owner of a relationship. If you want to make it the inverse side, check the Inverse option. Click OK.

See Also

Concepts:

Reference:

Web Resources: