@Entity
@Table(name = "Member")
public class Member {
...
}
@Entity
public class MemberA {
...
}
@Entity
@Table(name = "Member")
public class Member {
...
}
@Entity
public class MemberA {
...
}
@Entity
public class PkEx() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
@Entity
@SequenceGenerator(
name = "USER_PK_GENERATOR",
sequenceName = "USER_PK_SEQ",
initailValue = 1,
allocationSize = 50
)
public class PkEx() {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="USER_PK_GENERATOR")
private Long id;
private String name;
}
IDENTITY vs SEQUENCE
@GeneratedValue(strategy=GenerationType.TABLE)
CREATE TABLE CUSTOM_SEQUENCE {
sequence_name varchar(255) not null
, next_val bigint
, primary key (sequence_name)
}
@Entity
@TableGenerator(
name = "USER_SEQ_GENERATOR"
, table = "CUSTOM_SEQUENCE"
, pkColumnValue = "USER_SEQ"
, allocationSize = 1
)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "USER_SEQ_GENERATOR")
private long id;
}
@Embeddable
public class CompanyOrganizationKey implements Serializable {
@Column(name = "company_code")
private String companyCode;
@Column(name = "organization_code")
private String organizationCode;
}
@Entity(name = "company_organization")
public class CompanyOrganization {
@EmbeddedId
protected CompanyOrganizationKey companyOrganizationKey;
}
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
@NonNull
private String name;
@NonNull
private String pw;
private int age;
}
User user1 = new User(); // @NoArgsConstructor
User user2 = new User(1L, "user2", "1234", null); // @AllArgsConstructor
User user3 = new User("user3", "1234"); // @RequiredArgsConstructor
@Column
private String code;
@Column(length = 100)
private String name;
// @Column 은 생략 가능
private String desctiption;
@Column(precision = 11, scale = 2)
private BigDecimal amount;
@Column
private Integer size;
@Column(name = "register_date")
private LocalDateTime registerDate;
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
private String username;
@ManyToOne // 다대일
@JoinColumn(name = "TEAM_ID") // Member.team 필드를 TEAM_ID FK와 매핑
private Team team;
...
}
@Entity
public class Team {
@Id @GeneratedValue
@Column(name = "TEAM_ID")
private Long id;
private String name;
...
}
@Entity
public class Team {
@Id @GeneratedValue
@Column(name = "TEAM_ID")
private Long id;
private String name;
@OneToMany(mappedBy = "team")
private List<Member> members = new ArrayList<>();
...
}
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
private String username;
...
}
@Entity
public class Team {
@Id @GeneratedValue
@Column(name = "TEAM_ID")
private Long id;
private String name;
@OneToMany
@JoinColumn(name = "TEAM_ID")
private List<Member> members = new ArrayList<>();
...
}
양쪽이 서로 하나의 관계만을 가지는 경우
일대일 단방향
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "MEMBER_ID")
private Long id;
private String username;
@OneToOne
@JoinColumn(name = "LOCKER_ID")
private Locker locker;
}
@Entity
public class Locker {
@Id @GeneratedValue
@Column(name = "LOCKER_ID")
private Long id;
private String name;
}
@Entity
public class Locker {
@Id @GeneratedValue
@Column(name = "LOCKER_ID")
private Long id;
private String name;
@OneToOne
@JoinColumn(name = "MEMBER_ID")
private Member memeber;
}
@Entity
public class Member {
@Id @GeneratedValue
@Column(name = "MEMBER_ID")
private Long id;
private String username;
@OneToOne(mappedBy = "member")
private Locker locker;
}
@Entity
public class Order {
@Id @Column(name = "order_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
// referencedColumnName = "name"이며, name = "fk_name"인 경우
// 대상 테이블의 name 컬럼의 값을 FK로 사용하며, fk_name 컬럼에 저장
@JoinColumn(name = "member_id", referencedColumnName="name")
private Member member;
...
}
@ToString(exclude = "password")
public class User {
private Long id;
private String username;
private String password;
private int[] scores;
}
User user = new User();
user.setId(1L);
user.setUsername("dale");
user.setUsername("1234");
user.setScores(new int[]{80, 70, 100});
System.out.println(user); // User(id=1, username=1234, scores=[80, 70, 100])
https://gwonbookcase.tistory.com/37
https://velog.io/@gudnr1451/GeneratedValue-%EC%A0%95%EB%A6%AC