2011-12-20 16 views
10

Ho una domanda per progettare ManyToMany in EJB, come può un jointable avere una proprietà?
Ecco un esempio, gli studenti ei corsi sono ManyToMany, ogni studente ha molti corsi e molti studenti scelgono un corso.JPA ManyToMany, in che modo JoinTable può avere una proprietà?

@Entity 
    public class Student implements Serializable { 
     @Id 
     @GeneratedValue(strategy = GenerationType.AUTO) 
     Long id; 
     String name; 
     private Collection<Course> courses; 

     @ManyToMany(mappedBy = "students",cascade=CascadeType.ALL)  
     public Collection<Course> getCourses() { 
      return this.courses; 
     } 

     public void setCourses(Collection<Course> courses) { 
      this.courses = courses; 
     } 

    } 


    @Entity 
    public class Course implements Serializable { 
     @Id 
     @GeneratedValue(strategy = GenerationType.AUTO) 
     Long id; 
     String name; 
     private Collection<Student> students; 

     @ManyToMany(cascade=CascadeType.ALL) 
     @JoinTable(name = "Student_Course", 
     joinColumns = {@JoinColumn(name = "Course_ID", referencedColumnName = "id")}, 
     inverseJoinColumns = {@JoinColumn(name = "Student_ID", referencedColumnName = "id")}) 

     public Collection<Student> getStudents() { 
      return this.students; 
     } 

     public void setStudents(Collection<Student> students) { 
      this.students = students; 
     } 
    } 

Tuttavia, se ho una proprietà nel JoinTable, ad esempio ogni studente ha un punteggio per un corso. Come posso farlo in EJB con ManyToMany?
Mille grazie per la vostra attenzione!

+1

Se la vostra relazione ha proprietà, allora si dovrebbe modellare come entità. Questa domanda è quasi identica: http://stackoverflow.com/questions/7602386/mapping-value-in-junction-table-to-entity/7603036#7603036 Anche il nome dell'entità che rappresenta la relazione (CourseAssignment) si adatta abbastanza bene al tuo Astuccio. –

+3

Non è possibile, non è possibile aggiungere proprietà alla relazione. Se è necessario accedere alla proprietà nella tabella di join, la proprietà appartiene a qualche entità e, di conseguenza, è necessaria la terza entità. –

+0

scusate, l'ho corretto. Ho solo un'altra domanda, quando ho usato una classe @Embeddable come PK per jointable, non funziona (compilazione fallita), qualcuno ha detto che JPA2.0 non lo supporta! per favore guarda questo [jpa-eclipselink-manytomany-with-dat] (http://stackoverflow.com/questions/4013397/jpa-eclipselink-manytomany-with-data), c'è un modo per farlo funzionare? – seaguest

risposta

4

Non è possibile, non è possibile aggiungere proprietà alla relazione. Se è necessario accedere alla proprietà nella tabella di join, la proprietà appartiene a qualche entità e, di conseguenza, è necessaria la terza entità.

3

È possibile.

È sufficiente sostituire la mappatura molti-a-molti con la combinazione esplicita di mappature uno-a-molti e molti-a-uno tramite una terza entità, che rappresenterebbe l'associazione tra le due entità primarie (studente e corso nel tuo esempio).

Si prega di leggere i dettagli here

0

entità Molti di molte relazioni (Merchant e di servizio). Ciò può essere ottenuto utilizzando una terza entità come segue: -

@Entity 
@Table(name = "merchant") 
public class Merchant implements java.io.Serializable { 

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.merchant",targetEntity = MerchantService.class) 
    private Set<MerchantService> merchantServices = new HashSet<>(); 
} 

@Entity 
@Table(name = "merchant_service") 
@AssociationOverrides({ 
     @AssociationOverride(name = "pk.merchant", 
      joinColumns = @JoinColumn(name = "merchant_id")), 
     @AssociationOverride(name = "pk.service", 
      joinColumns = @JoinColumn(name = "service_id")) }) 
public class MerchantService implements java.io.Serializable { 

    @EmbeddedId 
    private MerchantServiceId pk = new MerchantServiceId(); 

    private boolean isActive; 

    public MerchantServiceId getPk() { 
     return pk; 
    } 

    public void setPk(MerchantServiceId pk) { 
     this.pk = pk; 
    } 

    @Transient 
    public Service getService() { 
     return getPk().getService(); 
    } 


    @Transient 
    public Merchant getMerchant() { 
     return getPk().getMerchant(); 
    } 


    public boolean isActive() { 
     return isActive; 
    } 

    public void setActive(boolean isActive) { 
     this.isActive = isActive; 
    } 

} 

@Embeddable 
public class MerchantServiceId implements java.io.Serializable { 

    private Merchant merchant; 
    private Service service; 

    @ManyToOne 
    public Merchant getMerchant() { 
     return merchant; 
    } 

    @ManyToOne 
    public Service getService() { 
     return service; 
    } 

} 

@Entity 
@Table(name = "service") 
public class Service implements java.io.Serializable { 

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.service",targetEntity = MerchantService.class) 
    private Set<MerchantService> merchantServices = new HashSet<>(); 

} 
Problemi correlati