2012-11-16 13 views
9

Pracuję z ostatnią wersją playframework (2.0.4) i ORMS Ebean. Oto mój uproszczony schemat dbEbean - Podstawowy klucz złożony zawierający klucze obce

TABLENAME (FIELD_NAME (, ...)) 
User (id) 
Group (id) 
UserGroup (user_id, group_id, is_active) 

Chciałbym stworzyć moje modele podmiotu tak:

@Entity 
public class UserGroup extends Model { 
    private static final long serialVersionUID = 1L; 

    @EmbeddedId 
    public UserGroupPK pk; 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "user_id", insertable = false, updatable = false) 
    public User user; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "group_id", insertable = false, updatable = false) 
    public Group group; 
} 

@Embeddable 
public class UserGroupPK implements Serializable{ 
    private static final long serialVersionUID = 1L; 

    public Long userId; 
    public Long groupId; 

    public UserGroupPK(Long userId, Long groupId) { 
     this.userId = userId; 
     this.groupId = groupId; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final UserGroupPK other = (UserGroupPK) obj; 
     if ((this.userId == null) ? (other.userId != null) : !this.userId.equals(other.userId)) { 
       return false; 
      } 
     if ((this.groupId == null) ? (other.groupId != null) : !this.groupId.equals(other.groupId)) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 3; 
     hash = 89 * hash + (this.userId != null ? this.userId.hashCode() : 0); 
     hash = 89 * hash + (this.groupId != null ? this.groupId.hashCode() : 0); 
     return hash; 
    } 
} 

Czy to dobrze dla ciebie. A jeśli dla tej tabeli pośredniej jest w porządku, co z użytkownikiem i jednostką grupy? Z góry dziękuję.

+0

Mapowanie, to jest w porządku. Ale powodzenia w wyszukiwaniu tej klasy. Nigdy nie udało mi się zmusić Ebeana do normalnej pracy z kluczami kompozytowymi. – kaqqao

Odpowiedz

4

Niektóre adnotacje nie wydają się właściwe, ale i tak mogą działać.

Gdybym był tobą chciałbym zrobić:

@Embeddable 
public class UserGroupPK implements Serializable{ 
    private static final long serialVersionUID = 1L; 

    @Column(name = "user_id") 
    public Long userId; 
    @Column(name = "group_id") 
    public Long groupId; 

Dla kolumny ManyToOne:

@ManyToOne 
@JoinColumn(name = "user_id", referenceColumnName = "id", nullable = false) // insertable and updatable by default are true, which I think are correct here 
public User user; 

// same for group 

W swojej jednostce użytkownika trzeba coś takiego:

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL) 
public Set<UserGroup> groups 

Kiedy robisz znaleźć, to jest jak

// find all users within a certain group 
Ebean.find(User.class) 
    .fetch("groups") 
    .where().eq("groups.groupId", "...").findList();