public class Group {
@Id
@Column(name = "groupidx")
@GeneratedValue(strategy=GenerationType.AUTO)
private int groupIdx;
}
public class Project {
@Id
@Column(name = "projectidx")
@GeneratedValue(strategy=GenerationType.AUTO)
private int projectIdx;
@OneToOne(mappedBy = "project", cascade = CascadeType.PERSIST)
Apple apple;
@OneToOne(mappedBy = "project", cascade = CascadeType.PERSIST)
Samsung samsung;
@ManyToOne(fetch = FetchType.EAGER)
@JsonBackReference
@JoinColumn(name = "groupidx", insertable=false, updatable=false)
private Group group;
}
public class Apple {
@Id
@Column(name = "id", unique = true, nullable = false)
private String id;
@Column(name = "projectidx", unique = true)
private int projectIdx;
@MapsId("projectIdx")
@OneToOne
@JoinColumn(name = "projectidx")
private Project project;
}
public class Samsung {
@Id
@Column(name = "id", unique = true, nullable = false)
private String id;
@Column(name = "projectidx", unique = true)
private int projectIdx;
@MapsId("projectIdx")
@OneToOne
@JoinColumn(name = "projectidx")
private Project project;
}
TestCode
@Transactional
@Test
public void groupAndProjectTest() {
Group group = groupRepository.findById(1).get();
List<Project> project = group.getProject();
assertNotNull(project.getProjectIdx);
assertNotNull(project.get(0).getApple().getApple***());
}
Group -> Project -> Apple or Samsung
group을 가져오면 해당 그룹이 가지고 있는 project, apple or samsung 데이터를 확인할 수 있다.
'JPA & DB' 카테고리의 다른 글
Hikari Option 정리 (0) | 2022.03.25 |
---|---|
JPA 트랜잭션 격리수준 (Isolation Level) 과 락 (Lock) (0) | 2022.01.10 |
JPA Persistence Context 그리고.. Flush / Lazy Loading / N+1 Problem (0) | 2021.11.15 |
Cascade 이해 및 orphanRemoval=true vs CascadeType.REMOVE (0) | 2020.05.15 |
[JPA] 복합키에서 equals () 및 hashCode () 구현하는 이유 (0) | 2020.03.06 |