@ManyToOne
@OneToMany
@OneToOne
@ManyToMany
(@JoinTable
)@JoinColumn
@OneToMany(mappedBy = "")
@OneToOne(mappedBy = "")
mappedBy
외래 키가 있는 쪽이 연관관계 주인이다.
연관관계 주인은
mappedBy
속성을 사용하지 않는다.연관관계 주인이 아니면,
mappedBy
속성을 사용해서 연관관계 주인의 필드 이름을 입력해야 한다.
@ManyToOne
@Entity
@Table(name = "TEAM_MEMBER")
@Getter
@Setter
@NoArgsConstructor
public class Member {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String name;
@ManyToOne
@JoinColumn(name = "TEAM_ID")
private Team team;
}
@JoinColumn(name = "TEAM_ID")
[Member.team](<http://Member.team>)
필드를 TEAM_ID
라는 컬럼으로 매핑Team
Entity 객체의 join column 설정@Entity
@Table(name = "TEAM_TABLE")
@Getter
@Setter
@NoArgsConstructor
public class Team {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
//@Column(name = "TEAM_ID")
private Long id;
private String name;
@OneToMany(mappedBy = "team")
private List<Member> members = new ArrayList<>();
}
@OneToMany
@Entity
@Table(name = "LEVEL")
@Getter
@Setter
public class Level {
@Id @GeneratedValue
@Column(name = "LEVEL_ID")
private Long id;
private String name;
@OneToMany
@JoinColumn(name = "LEVEL_ID") // MEMBER 테이블의 LEVEL_ID 컬럼
private List<Member> memberList = new ArrayList<>();
public void addMember(Member member) {
memberList.add(member);
}
}
Hibernate:
/* insert domain.team.Level
*/ insert
into
LEVEL
(name, LEVEL_ID)
values
(?, ?)
Hibernate:
/* create one-to-many row domain.team.Level.memberList */ update
MEMBER
set
LEVEL_ID=?
where
MEMBER_ID=?
@OneToMany
는 연관관계의 주인이 될 수 없다.@OneToMany
연관관계 주인이 될 수 있다.@Entity
@Table(name = "MEMBER")
@Getter
@Setter
@NoArgsConstructor
public class Member {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String name;
@ManyToOne
@JoinColumn(name = "LEVEL_ID", insertable = false, updatable = false)
private Level level;
}
@OneToOne
@Entity
@Table(name = "MEMBER")
@Getter
@Setter
@NoArgsConstructor
public class Member {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String name;
@OneToOne
@JoinColumn(name = "LOCKER_ID")
private Locker locker;
}
@Entity
@Table(name = "LOCKER")
@Setter
@Getter
@NoArgsConstructor
public class Locker {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "LOCKER_ID")
private Long id;
@Column
private String name;
@OneToOne(mappedBy = "locker")
private Member member;
}
JPA 2.0부터 일대다 단방향 관계에서 대상 테이블에 외래키가 있는 매핑을 허용한다.
하지만 일대일 단방향은 허용하지 않습니다.
@Entity
@Table(name = "MEMBER")
@Getter
@Setter
@NoArgsConstructor
public class Member {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String name;
@OneToOne(mappedBy = "member")
private LockerManager lockerManager;
}
@Entity
@Table(name = "LOCKER_MANAGER")
@Getter
@Setter
@NoArgsConstructor
public class LockerManager {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToOne
@JoinColumn(name = "MEMBER_ID")
private Member member;
}
@ManyToMany
@JoinTable
, @ManyToMany
@Entity
@Table(name = "MEMBER")
@Getter
@Setter
@NoArgsConstructor
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String name;
@ManyToMany
@JoinTable(name = "MEMBER_PRODUCT",
joinColumns = @JoinColumn(name = "MEMBER_ID"),
inverseJoinColumns = @JoinColumn(name = "PRODUCT_ID"))
private List<Product> productList = new ArrayList<>();
}
@Entity
@Table(name = "PRODUCT_INFO")
@Getter
@Setter
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany(mappedBy = "productList")
private List<Member> memberList = new ArrayList<>();
}
@JoinTable
을 통해 만들어진 연결 테이블은 JPA가 자동 생성한다.@Entity
@Table
@Getter
@Setter
public class MemberProduct {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "MEMBER_ID")
private Member member;
@ManyToOne
@JoinColumn(name = "PRODUCT_ID")
private Product product;
private Integer amount;
private Integer price;
}
@Entity
@Table(name = "MEMBER")
@Getter
@Setter
@NoArgsConstructor
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "MEMBER_ID")
private Long id;
@Column(name = "USERNAME")
private String name;
@OneToMany(mappedBy = "member")
private List<MemberProduct> memberProductList = new ArrayList<>();
}
@Entity
@Table(name = "PRODUCT")
@Getter
@Setter
@NoArgsConstructor
public class Product {
@Id
@GeneratedValue
@Column(name = "PRODUCT_ID")
private Long id;
private String name;
@OneToMany(mappedBy = "product")
private List<MemberProduct> memberProductList = new ArrayList<>();
}
Serializable
을 구현해야한다.equals
, hashCode
메소드를 구현해야 한다. (@EqualsAndHashCode
)@IdClass
, @EmbeddedId
활용@Entity
@Table(name = "MEMBER_PRODUCT")
@IdClass(value = MemberProductId.class)
@Getter
@Setter
@NoArgsConstructor
public class MemberProduct {
@Id
@ManyToOne
@JoinColumn(name = "MEMBER_ID")
private Member member;
@Id
@ManyToOne
@JoinColumn(name = "PRODUCT_ID")
private Product product;
private Integer amount;
private Integer price;
}
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class MemberProductId implements Serializable {
private Long member;
private Long product;
}