2017-04-10 1 views
0

스프링 부트로 봄 데이터 JPA (최대 절전 모드)를 사용 중입니다.스프링 부트 엔티티 (너무 큰 객체)

두 개의 데이터베이스 테이블이 있습니다. 첫째, 코스 테이블 & 초는 연구소 테이블입니다. 두 테이블 모두 약 20 개의 열이 있습니다.

이제 내 코스 테이블에는 연구소 테이블의 ID 인 parent_id라는 이름의 열이 있습니다.

JOIN 열을 지정하는 두 엔티티를 설계했으며 데이터도 가져오고 개체가 준비되었습니다.

하지만 문제는 내가 코스 객체 안에 완전한 연구소 객체를 원하지 않는다는 것입니다. 나는 단지 institute id (listing_id) & 이름을 원합니다. 엔티티를 어떻게 식별 할 수 있습니까?

코스 엔티티

@Entity 
@Table(name="courses") 
public class Course implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    private int id; 

    @Column(name="course_id") 
    private int courseId; 

    private String status 

    .. Other Properities... 

    @ManyToOne 
    @JoinColumns({ 
     @JoinColumn(name="parent_id", referencedColumnName="listing_id",insertable=false,updatable=false), 
     @JoinColumn(name="status", referencedColumnName="status",insertable=false, updatable=false), 
     }) 
    @Where(clause="status='live'") 
    private Institute institute; 

    .. Getters & setters 

} 

연구소 법인은

@Entity 
@Table(name="institutes") 
public class Institute implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    private int id; 

    @Column(name="listing_id") 
    private int listingId; 

    private String status; 

    @OneToMany(mappedBy="institute") 
    private List<Course> Courses; 

    .. Other Properties 

    .. Getters & Setters 

} 
+0

"빛"연구소 객체 만 원하는 수준은 무엇입니까? 엔티티 수준에서 또는 엔티티가 JSON으로 직렬화되고 REST API에서 반환되는 경우? – dunni

+0

엔티티 레벨에서 엔티티가 실제로 생성 될 때. 또한 엔티티가 JSON에 직렬화되고 REST API에서 반환 될 때 수행하는 방법을 알고 싶습니다. –

+0

엔티티의 특정 필드 만 원한다면 원하지 않는 다른 필드를 모두 제거하십시오. JSON 직렬화에 관해서는'@JsonView' 주석을 살펴보십시오. – dunni

답변

0

당신은 CourseDTO 객체를 선언 할 수 있습니다.

public class CourseDTO { 
    private int listingId; 
    private String name; 

    Get and set method ... 
} 

연구소 엔티티에는 많은 코스가 있습니다. pojo를 선택할 수 있습니다. 예,

@Query("select new com.pakcage.name.CourseDTO(c.id, c.courseId, i.listingId, i.name) from Course c join Institute i where c.id=:id") 
public CourseDTO fetchCourse(@Param("id") Long id); 
관련 문제