2012-11-16 24 views
9

है, मैं playframework (2.0.4) और ईबीन ओआरएम की अंतिम रिलीज के साथ काम कर रहा हूं।ईबीन - समग्र प्राथमिक कुंजी जिसमें विदेशी कुंजी

@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; 
    } 
} 

यह आपके लिए सही है: यहाँ मेरी सरलीकृत डाटाबेस स्कीमा

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

मैं इस तरह मेरी इकाई मॉडल बनाने के लिए, चाहते हैं। और यदि यह मध्यवर्ती तालिका के लिए ठीक है, तो उपयोगकर्ता और समूह इकाई के बारे में क्या? अग्रिम में धन्यवाद।

+0

मैपिंग वार, यह ठीक है। लेकिन इस वर्ग के लिए एक खोजक बनाने के साथ शुभकामनाएं। मैं आम तौर पर संयुक्त कुंजी के साथ काम करने में कामयाब नहीं रहा। – kaqqao

उत्तर

4

कुछ टिप्पणियां सही नहीं लगती हैं लेकिन वैसे भी काम कर सकती हैं।

मैं क्या करना होगा अगर मैं तुम्हें थे:

@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; 

ManyToOne स्तंभ के लिए:

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

जब आप ऐसा करेंगे:

@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 

अपने प्रयोक्ता इकाई में आप की तरह कुछ की जरूरत है पाते हैं, यह

जैसा है
संबंधित मुद्दे