Home >Java >javaTutorial >How do you define relationships between entities in Hibernate 4 and Spring using annotations?

How do you define relationships between entities in Hibernate 4 and Spring using annotations?

Susan Sarandon
Susan SarandonOriginal
2024-11-03 13:30:30495browse

How do you define relationships between entities in Hibernate 4 and Spring using annotations?

Using Annotations for Defining Relationships in Hibernate 4 and Spring

Uni-Directional One-to-One Relationship

To create a uni-directional one-to-one relationship, use the @OneToOne annotation on the owning class. Configure the @JoinColumn annotation to specify the name of the column that will hold the foreign key in the table of the dependent class.

Example:

<code class="java">public class Foo {
    private Long fooId;

    @OneToOne
    private Bar bar;
}

public class Bar {
    private Long barId;
    // No corresponding mapping to Foo.class
}</code>

Bi-Directional One-to-One Relationship

For a bi-directional one-to-one relationship, annotate both classes with @OneToOne. Use the @JoinColumn annotation on the owning class to specify the foreign key column name. On the dependent class, use the @JoinColumn annotation to map the foreign key back to the owning class.

Example:

<code class="java">public class Foo {
    private Long fooId;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "barId")
    private Bar bar;
}

public class Bar {
    private Long barId;

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

Uni-Directional One-to-Many Relationship with User-Managed Join Table

For a uni-directional one-to-many relationship using a user-managed join table, create a join table class that maps the relationship between the two classes. Annotate the join table with @Entity and @Table and map the columns to the primary keys of the parent and child classes.

Example:

<code class="java">public class Foo {
    private Long fooId;

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

public class Bar {
    private Long barId;

    // No mapping specified here.
}

@Entity
@Table(name="FOO_BAR")
public class FooBar {
    private UUID fooBarId;

    @ManyToOne
    @JoinColumn(name = "fooId")
    private Foo foo;

    @ManyToOne
    @JoinColumn(name = "barId")
    private Bar bar;

    // You can store other objects/fields on this table here.
}</code>

Bi-Directional One-to-Many Relationship Using Foreign Key Mapping

For a bi-directional one-to-many relationship using foreign key mapping, annotate the owning class with @OneToMany and the dependent class with @ManyToOne. Use the @JoinColumn annotation to specify the column name that will hold the foreign key in the dependent class table.

Example:

<code class="java">public class Foo {
    private Long fooId;

    @OneToMany(mappedBy = "bar")
    private List<Bar> bars;
}

public class Bar {
    private Long barId;

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

Bi-Directional Many-to-Many Relationship Using Hibernate-Managed Join Table

For a bi-directional many-to-many relationship using a Hibernate-managed join table, annotate each class with @OneToMany. Create a join table class that maps the relationship between the two classes. Annotate the join table with @Entity and @Table and map the columns to the primary keys of the parent and child classes.

Example:

<code class="java">public class Foo {
    private Long fooId;

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

public class Bar {
    private Long barId;

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

Bi-Directional Many-to-Many Relationship Using User-Managed Join Table Object

For a bi-directional many-to-many relationship using a user-managed join table object, create a join table class that maps the relationship between the two classes. Annotate the join table with @Entity and @Table and map the columns to the primary keys of the parent and child classes. Annotate each class with @OneToMany and map the fields to the join table class.

Example:

<code class="java">public class Foo {
    private Long fooId;

    @OneToMany(mappedBy = "bar")
    private List<FooBar> bars;
}

public class Bar {
    private Long barId;

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

@Entity
@Table(name="FOO_BAR")
public class FooBar {
    private UUID fooBarId;

    @ManyToOne
    @JoinColumn(name = "fooId")
    private Foo foo;

    @ManyToOne
    @JoinColumn(name = "barId")
    private Bar bar;

    // You can store other objects/fields on this table here.
}</code>

Determining Cascade Direction

Hibernate can cascade operations either or both ways in a bi-directional relationship. Cascade changes in the desired direction by marking the @OneToMany annotation on the owning class. Generally, it is advisable to mark the cascades on the owning side of the relationship.

Orphan Removal

Orphan removal ensures that orphaned objects that are no longer associated with a parent object are automatically deleted from the database. Enable orphan removal by setting orphanRemoval = true on the @OneToMany annotation.

Dealing with LazyInitializationException

LazyInitializationException occurs when an entity that contains a lazily fetched collection is accessed before the collection is initialized. To resolve, initialize the collection explicitly using Hibernate.initialize() or use fetchType = FetchMode.EAGER on the @OneToMany annotation to eagerly load the collection.

The above is the detailed content of How do you define relationships between entities in Hibernate 4 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