https://stackoverflow.com/questions/13032948/how-to-create-and-handle-composite-primary-key-in-jpa.
1. @EmbeddedId 활용.
@Entity
public class YourEntity {
@EmbeddedId
private MyKey myKey;
@Column(name = "ColumnA")
private String columnA;
/** Your getters and setters **/
}
@Embeddable
public class MyKey implements Serializable {
@Column(name = "Id", nullable = false)
private int id;
@Column(name = "Version", nullable = false)
private int version;
/** getters and setters **/
}
---
2. @IdClass 활용.
@Entity
@IdClass(MyKey.class)
public class YourEntity {
@Id
private int id;
@Id
private int version;
}
public class MyKey implements Serializable {
private int id;
private int version;
}
필자의 경우 getter/setter가 필요없는 2. 방법을 활용했다.
'WIL(What I Learned)' 카테고리의 다른 글
java input (Scanner class) (0) | 2021.08.16 |
---|---|
okky Spring IoC (0) | 2021.08.05 |
Entity에 무작위의 문자열을 id값으로 지정하기 (0) | 2021.07.22 |
TIL (0) | 2021.07.13 |
자바 ArrayList에서 요소의 수 세기 (0) | 2021.07.13 |