You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

연관있는 두 테이블이 복합키로 지정이 되어있으나, 외래키설정이 되어있지 않을때

설계상의 문제이거나, 관계도가 있는 왼쪽에비해 오른쪽에대해 무결성을 지킬필요가 없을때

존재할수 있으며 이러한 테이블의 종류가 운영이지속될수록 많이 생긴다는것입니다.


관련 영문 키워드:

mapping compositekey primary and non forign


샘플 DDL

ItemlInfo

CREATE TABLE `db_example2`.`iteminfo` (
  `itemtype` CHAR(1) NOT NULL,
  `itemno` INT NOT NULL,
  `itemname` VARCHAR(45) NULL,
  PRIMARY KEY (`itemtype`, `itemno`));


ItemStatics

CREATE TABLE `itemstatics` (
  `itemtype` char(1) NOT NULL,
  `itemno` int(11) NOT NULL,
  `viewcnt` int(11) DEFAULT NULL,
  `salecnt` int(11) DEFAULT NULL,
  `statictype` varchar(45) NOT NULL,
  PRIMARY KEY (`itemtype`,`itemno`,`statictype`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


Entity 설계


복합키정의

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;
	}

}


아이템통계

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;

@Entity
public class ItemStatics {
	@EmbeddedId 
	ItemId staticid;
	
	int viewcnt;
}



아이템정보

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;
	
	String itemname;
	
    @MapsId("staticid") //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;

}


조회하기(조인기능 탑재)

	@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);			
		});		
	}




  • No labels