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 데이터를 확인할 수 있다. 

 

 

 

 

+ Recent posts