2016-08-26 6 views
1

저는 ORM을 처음 접했고 다음 시나리오를 통해 아이디어를 제안 할 수 있습니다.개체 관계형 매핑

나는

@Entity 
public class Chat {  

    @Id 
    @GeneratedValue 
    @Column(name="Id") 
    private int id; 

    @ManyToOne 
    private User userSent; 

    @ManyToOne 
    private User userRecieve; 

    private Date time; 

    @Column(name="message", length=50) 
    private String message; 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 
    public Date getTime() { 
     return time; 
    } 

    public void setTime(Date time) { 
     this.time = time; 
    } 

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 
} 

울부 짖는 소리로 채팅 클래스가 사용자 클래스와

@Entity 
public class User { 

    @Id 
    @GeneratedValue 
    private Integer id; 

    private String name; 

    private String email; 

    private String password; 


    @OneToMany 
    private List<Chat> chats; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 
    public List<Chat> getChats() { 
     return chats; 
    } 

    public void setChats(List<Chat> chats) { 
     this.chats = chats; 
    } 
} 

우는 소리 그리고 내 문제는 사용자가 많은 채팅을 가지고 있으며 채팅은 사용자의 2 가지 유형으로있다 수신자와 발신자이지만 둘 다 특정 채팅에 속합니다.

그리고 내 질문은 어떻게 관계를 제공하는 방법을 선언 할 수 있습니다. 그것은 불가능 미리

+0

그래서 사용자의 '채팅'목록에는 사용자가 발신자이거나 수신자 인 모든 채팅이 포함되어 있습니까? –

+0

Bidirecttional 일대 다 연관은 설명서에서 다룹니다. http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#associations-one-to-many-bidirectional –

+0

하지만이 오히려 "2 대 다수"관계입니다. 양방향으로 만드는 것은 매우 복잡합니다. 단방향으로 만들고'ChatRepository'에'findByUser' 또는'findByUserId' 메소드를 제공합니다. –

답변

0

감사는 간단한 방식으로 JPA (즉 모두 별개의 분야이다) 양방향 여러 개의 행 관계를 표현한다.

이 문제를 해결하는 가장 간단한 방법은 User에서 chats 필드를 제거하여 관계를 단방향으로 만드는 것입니다. 그런 다음 Chat에 대한 저장소/DAO에서 당신은하지 User 실체 자체를 통해,

SELECT Chat c WHERE c.userSent = :user OR c.userReceive = :user 

같은 쿼리가 여전히 특정 사용자에 대해 채팅의 컬렉션을 얻을 수있는이 방법을 기능 findByUser을 제공합니다.


또 다른 해결책은 많은에 관계 많은하고 채팅에 참여하는 사용자의 유형을 나타냅니다의 관계에 속성을 추가하는 것입니다. 당신은 다른 엔티티 '게터

@Entity 
public class Chat { 
    @OneToMany 
    private Set<ChatParticipation> participations; 

    public User getSender() { 
    return participations.stream() 
     .filter(p -> p.getType() == ChatParticipation.Type.SENDER) 
     .map(ChatParticipation::getUser) 
     .findFirst().get(); 
    } 

    public User getReceiver() { 
    return participations.stream() 
     .filter(p -> p.getType() == ChatParticipation.Type.RECEIVER) 
     .map(ChatParticipation::getUser) 
     .findFirst().get(); 
    } 
} 

@Entity 
public class User { 
    @OneToMany 
    private Set<ChatParticipation> participations; 

    public Set<Chat> getChats() { 
    return participations.stream() 
     .map(ChatParticipation::getChat) 
     .collect(Collectors.toSet()); 
    } 
} 

당신은 아마 게터의 결과를 캐시한다을

@Entity 
public class ChatParticipation { 
    public static enum Type { 
    SENDER, 
    RECEIVER 
    } 

    private Chat chat; 
    private User user; 
    private Type type; 
} 

같은 중간 엔티티를 생성하고 수정해야 할 것입니다. 또한 세터를위한 해결책을 제시해야합니다.