hibernate
어노테이션을 사용하여 Hibernate 엔티티 관계
수색…
매개 변수
주석 | 세부 |
---|---|
@OneToOne | 해당 객체와 일대일 관계를 지정합니다. |
@OneToMany | 여러 개체에 매핑되는 단일 개체를 지정합니다. |
@ManyToOne | 단일 개체에 매핑되는 개체의 컬렉션을 지정합니다. |
@Entity | 데이터베이스 테이블에 매핑되는 개체를 지정합니다. |
@Table | 이 개체가 매핑하는 데이터베이스 테이블도 지정합니다. |
@JoinColumn | foregin 키가 저장되는 열을 지정합니다. |
@JoinTable | 외부 키를 저장하는 중간 테이블을 지정합니다. |
사용자 관리 조인 테이블 객체를 사용하는 많은 양방향
@Entity
@Table(name="FOO")
public class Foo {
private UUID fooId;
@OneToMany(mappedBy = "bar")
private List<FooBar> bars;
}
@Entity
@Table(name="BAR")
public class Bar {
private UUID 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.
}
사용자가 관리하는 중간 조인 테이블을 사용하여 많은 Foo
객체와 많은 Bar
객체 간의 양방향 관계를 지정합니다.
Foo
오브젝트는 FOO
라는 테이블에 행으로 저장됩니다. Bar
오브젝트는 BAR
테이블에 행으로 저장됩니다. Foo
와 Bar
오브젝트 간의 관계는 FOO_BAR
테이블에 저장됩니다. 응용 프로그램의 일부로 FooBar
객체가 있습니다.
관계가 작성된 날짜와 같은 추가 정보를 조인 오브젝트에 저장하려는 경우에 일반적으로 사용됩니다.
Hibernate 관리 조인 테이블을 사용하는 양방향 대다수에서 많음
@Entity
@Table(name="FOO")
public class Foo {
private UUID fooId;
@OneToMany
@JoinTable(name="FOO_BAR",
joinColumns = @JoinColumn(name="fooId"),
inverseJoinColumns = @JoinColumn(name="barId"))
private List<Bar> bars;
}
@Entity
@Table(name="BAR")
public class Bar {
private UUID barId;
@OneToMany
@JoinTable(name="FOO_BAR",
joinColumns = @JoinColumn(name="barId"),
inverseJoinColumns = @JoinColumn(name="fooId"))
private List<Foo> foos;
}
Hibernate가 관리하는 중간 조인 테이블을 사용하여 많은 Foo
객체와 많은 Bar
객체 간의 관계를 지정합니다.
Foo
오브젝트는 FOO
라는 테이블에 행으로 저장됩니다. Bar
오브젝트는 BAR
테이블에 행으로 저장됩니다. Foo
와 Bar
오브젝트 간의 관계는 FOO_BAR
테이블에 저장됩니다. 그러나 이는 응용 프로그램의 일부로 FooBar
객체가 없음을 의미합니다.
외래 키 매핑을 사용한 양방향 일대 다 관계
@Entity
@Table(name="FOO")
public class Foo {
private UUID fooId;
@OneToMany(mappedBy = "bar")
private List<Bar> bars;
}
@Entity
@Table(name="BAR")
public class Bar {
private UUID barId;
@ManyToOne
@JoinColumn(name = "fooId")
private Foo foo;
}
하나의 Foo
객체와 외래 키를 사용하는 여러 Bar
객체 간의 양방향 관계를 지정합니다.
Foo
오브젝트는 FOO
라는 테이블에 행으로 저장됩니다. Bar
오브젝트는 BAR
테이블에 행으로 저장됩니다. 외부 키는 fooId
라는 C 럼의 BAR
테이블에 저장됩니다.
Foo.class가 관리하는 양방향 일대일 관계
@Entity
@Table(name="FOO")
public class Foo {
private UUID fooId;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "barId")
private Bar bar;
}
@Entity
@Table(name="BAR")
public class Bar {
private UUID barId;
@OneToOne(mappedBy = "bar")
private Foo foo;
}
외래 키를 사용하여 하나의 Foo
객체와 하나의 Bar
객체 사이의 양방향 관계를 지정합니다.
Foo
오브젝트는 FOO
라는 테이블에 행으로 저장됩니다. Bar
오브젝트는 BAR
테이블에 행으로 저장됩니다. 외래 키는 barId
라는 열의 FOO
테이블에 저장됩니다.
mappedBy
값은 열 이름이 아니라 객체의 필드 이름입니다.
사용자 관리 조인 테이블을 사용한 단방향 1 대 다수 관계
@Entity
@Table(name="FOO")
public class Foo {
private UUID fooId;
@OneToMany
@JoinTable(name="FOO_BAR",
joinColumns = @JoinColumn(name="fooId"),
inverseJoinColumns = @JoinColumn(name="barId", unique=true))
private List<Bar> bars;
}
@Entity
@Table(name="BAR")
public class Bar {
private UUID 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", unique = true)
private Bar bar;
//You can store other objects/fields on this table here.
}
사용자가 관리하는 중간 조인 테이블을 사용하여 하나의 Foo
객체와 여러 Bar
객체 간의 단방향 관계를 지정합니다.
이는 ManyToMany
관계와 유사하지만 대상 외래 키에 unique
제한 조건을 추가하면 OneToMany
라는 것을 적용 할 수 있습니다.
Foo
오브젝트는 FOO
라는 테이블에 행으로 저장됩니다. Bar
오브젝트는 BAR
테이블에 행으로 저장됩니다. Foo
와 Bar
오브젝트 간의 관계는 FOO_BAR
테이블에 저장됩니다. 응용 프로그램의 일부로 FooBar
객체가 있습니다.
Foo
객체에 대한 Bar
객체의 매핑이 없다. Bar
객체는 Foo
객체에 영향을 미치지 않고 자유롭게 조작 할 수 있습니다.
수행 할 수있는 Role
의 목록을 가진 User
객체를 설정할 때 Spring Security와 함께 매우 일반적으로 사용됩니다. 당신은 삭제 폭포에 대해 걱정할 필요없이 사용자에게 역할을 추가 및 제거 할 수있는 Role
의를.
단방향 일대일 관계
@Entity
@Table(name="FOO")
public class Foo {
private UUID fooId;
@OneToOne
private Bar bar;
}
@Entity
@Table(name="BAR")
public class Bar {
private UUID barId;
//No corresponding mapping to Foo.class
}
하나의 Foo
객체와 하나의 Bar
객체 사이의 단방향 관계를 지정합니다.
Foo
오브젝트는 FOO
라는 테이블에 행으로 저장됩니다. Bar
오브젝트는 BAR
테이블에 행으로 저장됩니다.
Foo
객체에 대한 Bar
객체의 매핑이 없다. Bar
객체는 Foo
객체에 영향을 미치지 않고 자유롭게 조작 할 수 있습니다.