Home  >  Article  >  Java  >  How Can You Define Relationships in Hibernate and Spring Using Annotations?

How Can You Define Relationships in Hibernate and Spring Using Annotations?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-03 09:10:03732browse

How Can You Define Relationships in Hibernate and Spring Using Annotations?

Using Annotations to Define Relationships in Hibernate 4 and Spring

Uni-Directional and Bi-Directional Relationships

One-to-One Relationships

Uni-Directional:

<code class="java">class Foo {
    private Bar bar;
}

class Bar {
}</code>

Bi-Directional (managed by Foo class):

<code class="java">class Foo {
    @OneToOne(cascade = CascadeType.ALL)
    private Bar bar;
}

class Bar {
    @OneToOne(mappedBy = "bar")
    private Foo foo;
}</code>

One-to-Many Relationships

Uni-Directional using User Managed Join Table:

<code class="java">class Foo {
    @OneToMany
    @JoinTable(name = "FOO_BAR",
        joinColumns = {@JoinColumn(name = "fooId")},
        inverseJoinColumns = {@JoinColumn(name = "barId")})
    private List<Bar> bars;
}

class Bar {
    // No corresponding mapping to Foo.class
}

@Entity
@Table(name = "FOO_BAR")
class FooBar {
    private UUID fooBarId;
    private Foo foo;
    private Bar bar;
}</code>

Bi-Directional using Foreign Key Mapping:

<code class="java">class Foo {
    @OneToMany(mappedBy = "bar")
    private List<Bar> bars;
}

class Bar {
    @ManyToOne
    @JoinColumn(name = "fooId")
    private Foo foo;
}</code>

Many-to-Many Relationships

Bi-Directional using Hibernate Managed Join Table:

<code class="java">class Foo {
    @OneToMany
    @JoinTable(name = "FOO_BAR",
        joinColumns = {@JoinColumn(name = "fooId")},
        inverseJoinColumns = {@JoinColumn(name = "barId")})
    private List<Bar> bars;
}

class Bar {
    @OneToMany
    @JoinTable(name = "FOO_BAR",
        joinColumns = {@JoinColumn(name = "barId")},
        inverseJoinColumns = {@JoinColumn(name = "fooId")})
    private List<Foo> foos;
}</code>

Bi-Directional using User Managed Join Table:

<code class="java">class Foo {
    @OneToMany(mappedBy = "bar")
    private List<FooBar> bars;
}

class Bar {
    @OneToMany(mappedBy = "foo")
    private List<FooBar> foos;
}

@Entity
@Table(name = "FOO_BAR")
class FooBar {
    private UUID fooBarId;
    private Foo foo;
    private Bar bar;
}</code>

Determining Relationship Ownership

  • Generally, the object with the collection owns the relationship.

Choosing FetchType

  • Default: LAZY
  • Use EAGER for collections that are often used.
  • Consider using Hibernate.initialize() or FetchMode.SUBSELECT for lazily loaded collections needed in a specific method.

Determining Cascade Direction

  • Cascade operations only going in one direction unless specified.
  • Mark the @OneToMany annotation for cascade in the owning side of the relationship.

Orphan Removal

  • OrphanRemoval = true: Removes orphaned objects that are no longer referenced in any other relationship.

The above is the detailed content of How Can You Define Relationships in Hibernate and Spring Using Annotations?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn