Page History
...
Code Block | ||||
---|---|---|---|---|
| ||||
CREATE TABLE `db_example2`.`itemstatics` ( `itemtype` CHARchar(1) NOT NULL, `itemno` INTint(11) NOT NULL, `viewcnt` int(11) INTDEFAULT NULL, `salecnt` INT int(11) DEFAULT NULL, `statictype` varchar(45) NOT NULL, PRIMARY KEY (`itemtype`, `itemno`,`statictype`)); ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
Entity 설계
복합키정의
Code Block | ||||
---|---|---|---|---|
| ||||
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class ItemId implements Serializable {
@Column(columnDefinition="char(7)")
private String itemtype;
private int itemno;
public ItemId() {
}
public ItemId(String itemtype, int itemno) {
this.itemtype = itemtype;
this.itemno = itemno;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = itemtype.hashCode() + itemno;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ItemId other = (ItemId) obj;
if (itemtype == null) {
if (other.itemtype != null)
return false;
} else if (!itemtype.equals(other.itemtype))
return false;
if (itemno != other.itemno)
return false;
return true;
}
public String getItemtype() {
return itemtype;
}
public void setItemtype(String itemtype) {
this.itemtype = itemtype;
}
public int getItemno() {
return itemno;
}
public void setItemno(int itemno) {
this.itemno = itemno;
}
} |
...
Code Block | ||||
---|---|---|---|---|
| ||||
import javax.persistence.EmbeddedId; import javax.persistence.Entity; @Entity public class ItemStatics { @EmbeddedId ItemId idstaticid; int viewcnt; } |
아이템정보
Code Block | ||||
---|---|---|---|---|
| ||||
import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import javax.persistence.ManyToOne; import javax.persistence.MapsId; @Entity public class ItemInfo { @EmbeddedId ItemId itemid; @Column(nullable=false,insertable = false,updatable=false) private String itemtype; @Column(nullable=false,insertable = false,updatable=false) private int itemno; String itemname; @MapsId("idstaticid") //references EmbeddedId's property @JoinColumns({ @JoinColumn( insertable = false, updatable = false, name = "itemtype", referencedColumnName = "itemtype"), @JoinColumn( insertable = false, updatable = false, name = "itemno", referencedColumnName = "itemno") }) @ManyToOne private ItemStatics statics; } |
조회하기(조인기능 탑재)
Code Block | ||||
---|---|---|---|---|
| ||||
@Autowired ItemInfoRepository itemRepo; public void allTest() { itemRepo.findAll().forEach(item -> { String itemString = String.format("%s %d",item.getItemname() ,item.getStatics().getViewcnt() ); System.out.println(itemString); }); } |
...