티스토리 뷰

http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate


@ID 방식과 @Embeddable 방식이 있는데 후자의 방식으로 코딩했다.(이유는 @Id 방식으로 했을 때 에러가 나서?)

TagBusy의 PK 정보를 TagBusyId가 가지고 있다(3가지 복합키)


@Entity
@Table(name = "tag_busy")
public class TagBusy {
@EmbeddedId
private TagBusyId tagBusyId; //PK
@Column(name = "reg_date", nullable = true, insertable = false, updatable = false)
private Date regDate; //등록일시

public TagBusyId getTagBusyId() {
return tagBusyId;
}

public void setTagBusyId(TagBusyId tagBusyId) {
this.tagBusyId = tagBusyId;
}

public Date getRegDate() {
return regDate;
}

public void setRegDate(Date regDate) {
this.regDate = regDate;
}
}




@Embeddable
public class TagBusyId implements Serializable {
@ManyToOne(optional = false)
@JoinColumn(name = "tag_no", nullable = false)
private Tag tag; //태그
@Column(name = "cnts_no", nullable = false)
private int cntsNo; //컨텐츠번호
@Column(name = "cnts_type", nullable = false)
private int cntsType; //컨텐츠타입

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

TagBusyId tagBusyId = (TagBusyId) o;

if (cntsNo != tagBusyId.cntsNo) return false;
if (cntsType != tagBusyId.cntsType) return false;
if (tag != null ? !tag.equals(tagBusyId.tag) : tagBusyId.tag != null) return false;

return true;
}

@Override
public int hashCode() {
int result = tag != null ? tag.hashCode() : 0;
result = 31 * result + cntsNo;
result = 31 * result + cntsType;
return result;
}

public Tag getTag() {
return tag;
}

public void setTag(Tag tag) {
this.tag = tag;
}

public int getCntsNo() {
return cntsNo;
}

public void setCntsNo(int cntsNo) {
this.cntsNo = cntsNo;
}

public int getCntsType() {
return cntsType;
}

public void setCntsType(int cntsType) {
this.cntsType = cntsType;
}
}


댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함