IntelliJ IDEA 14.0.0 Web Help

Creating and Editing Relationships

When relationships are created between Hibernate elements:

  • The respective annotated properties are added to the source files of the entity classes, marked with gutter icons in the editor:
    hibernateAddRelationship1.png
  • Links appear in the Hibernate ER diagram of a session factory:
    hibernateAddRelationship2.png
  • Nodes are added to the session factory in the Persistence tool window:
    hibernateAddRelationship3.png

IntelliJ IDEA suggests several ways to create relationships between entities:

To create a relationship using the editor

  1. Open the desired entity class for editing.
  2. Type field declaration for a new relationship. For example,
    private Product suppliedBy;
  3. Create getter and setter methods. To do that, press Alt+Insert, and select Getter and Setter from the suggestion list. For example:
    public Product getSuppliedBy() { return suppliedBy; } public void setSuppliedBy(Product suppliedBy) { this.suppliedBy = suppliedBy; }
  4. Annotate the field with the desired relationship annotation, and specify annotation parameters, if required. For example:
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false, mappedBy = "id")

    If a parameter value is a reference to an entity of a data source, IntelliJ IDEA validates these references against the data source mapped in the facet settings.

To create relationships using drag-and-drop in the hibernate er diagram

  1. In the Persistence tool window, under the desired Hibernate-enabled module, right-click the desired session factory, and click erDiagramIcon.png on the toolbar.
  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. Click OK.

To create a relationship using an entity context menu

  1. In the Persistence tool window, under the desired Hibernate-enabled module, right-click the desired session factory and choose New | <relationship type> on the context menu.
  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

Last modified: 4 December 2014
comments powered by Disqus