Uni-Arah:
<code class="java">class Foo { private Bar bar; } class Bar { }</code>
Dwi Arah (diuruskan oleh kelas Foo):
<code class="java">class Foo { @OneToOne(cascade = CascadeType.ALL) private Bar bar; } class Bar { @OneToOne(mappedBy = "bar") private Foo foo; }</code>
Sehala Arah menggunakan Jadual Sertaan Terurus Pengguna:
<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>
Dwi Arah menggunakan Pemetaan Kunci Asing:
<code class="java">class Foo { @OneToMany(mappedBy = "bar") private List<Bar> bars; } class Bar { @ManyToOne @JoinColumn(name = "fooId") private Foo foo; }</code>
Dua Arah menggunakan Jadual Sertaan Terurus Hibernate:
<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>
Dwi Arah menggunakan Jadual Sertaan Terurus Pengguna:
<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>
Atas ialah kandungan terperinci Bagaimana Anda Boleh Mentakrifkan Perhubungan dalam Hibernate dan Spring Menggunakan Anotasi?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!