@OneToMany,@ManyToOne 컬렉션에서 Map, MapKeyColumn 사용예문
열공하세요~
1. Emp.java
package demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Entity
@Table(name = "emp")
@RequiredArgsConstructor
@Getter
@Setter
@ToString
public class Emp {
@Id
@GeneratedValue
private Long empno;
private final String ename;
@ManyToOne
@JoinColumn(name = "deptno")
private final Dept dept;
public Emp(String ename) {
this.ename = ename;
dept = null;
}
}
2. Dept.java
package demo.model;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Entity
@Table(name = "dept")
@RequiredArgsConstructor
@Getter
@Setter
@ToString(exclude="emps")
public class Dept {
@Id
@GeneratedValue
private Long deptno;
@NonNull
private String dname;
@NonNull
@OneToMany(mappedBy = "dept", cascade = CascadeType.ALL)
@MapKeyColumn(name = "id", nullable=true)
private Map<Integer, Emp> emps;
public Dept(String dname) {
this.dname = dname;
emps = null;
}
}
3. DeptRepository.java
package demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import demo.model.Dept;
public interface DeptRepository extends JpaRepository<Dept, Long> {
}
4. 스프링 부트 메인 ManytooneApplication.java
package demo;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import demo.model.Dept;
import demo.model.Emp;
import demo.repository.DeptRepository;
@SpringBootApplication
public class ManytooneApplication implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(ManytooneApplication.class);
@Autowired
private DeptRepository deptRepository;
@PersistenceContext
private EntityManager em;
public static void main(String[] args) {
SpringApplication.run(ManytooneApplication.class, args);
}
@Override
@Transactional
public void run(String...strings) throws Exception {
//Dept에 1번부서가 만들어지고, Emp의 deptno는 1로 입력됨
Dept d1 = new Dept("교육팀");
Emp e1 = new Emp("김교육", d1);
Emp e2 = new Emp("나교육", d1);
d1.setEmps(new HashMap<Integer, Emp>() {
{
put(1, e1);
put(2, e2);
}
});
//Dept에 2번 부서가 만들어지고 Emp의 deptno는 NULL로 입력
Dept d2 = new Dept("개발팀", new HashMap<Integer, Emp>() {
{
put(1, new Emp("김개발"));
put(2, new Emp("나개발"));
}
});
//Dept, Emp 저장
deptRepository.save(new HashSet<Dept>() {
{
add(d1);
add(d2);
}
});
for(Dept d : deptRepository.findAll()) {
if (d != null) {
logger.info(d.toString());
}
}
//------------- 1번 부서 및 부서원 로드
Dept d3 = deptRepository.findOne(1L);
Map<Integer, Emp> emps = d3.getEmps();
for(Map.Entry<Integer, Emp> entry : emps.entrySet()) {
logger.info("[" + entry.getKey() + ":" + entry.getValue() + "]");
}
//------------- 1번부서 삭제, 1번부서원들도 같이 삭제된다.
deptRepository.delete(d3);
}
}
댓글 없음:
댓글 쓰기