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>
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>
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>
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!