2011-09-02 3 views
1

Java 및 Hibernate를 사용하여 문서 수정 추적 시스템을 구현하려고합니다. 문서에 대한 모든 모드는 autor 및 timestamp와 함께 저장됩니다. 문서 작성자를 얻으려면 가장 빠른 모드를 만들어야합니다. 여기있다 : 나는 적어도 엔티티에서 getAutor() 방법에서 쿼리를 실행하지 않는 게 좋을쿼리에 의해 정의 된 Hibernate 엔티티 프로퍼티

@Entity 
@Table(name="Modifications") 
public class Modification 
{ 
    String autor; 
    Date dateTime; 
    Document document; 

    @ManyToOne 
    public Document getDocument() { 
    ... 
    } 

    // Author of this mod 
    @Column(name="Autor") 
    public String getAutor() { 
     ... 
    } 

    // and other properties 
} 

@Entity 
@Table(name="Documents") 
public class Document 
{ 
    private List<Modification> modifications; 

    @OneToMany(mappedBy="document") 
    public List<Modification> getModifications() { 
    ... 
    } 

    // this property is question about. How should I implement this with hibernate? 
    //I don't want to store "autor" field in entity, 
    //it should be determined with query to modifications table. 
    // But I can't find any example of defining properties this way... 
    // So I need your advices :) 
    public String getAutor() { 
    ... 
    } 

    // other properties 
} 

답변

1

. IMO entites는 쿼리 자체를 실행하지 않아야합니다.

그렇다면 필요에 따라 작성자에게 제공하는 일부 서비스/DAO에서 메소드를 제공하지 않는 이유는 무엇입니까? 또는 문서의 작성자에 대한 임시 필드를 만들고 dao가 채우도록 할 수 있습니다. 이렇게하면 작성자를 한 번만로드하고 정보에 여러 번 액세스해야합니다.

1

저는 주석을 통해이를 수행하려고합니다. 수정 목록에 @OrderBy를 설정하고 getAutor() 함수가 첫 번째 (또는 마지막) 엔티티를 검색하도록 할 수 있습니다.

나는 토마스의 방법론이 더 좋지만 논리를 더 잘 구분할 수있다.

관련 문제