2009-05-28 10 views
2

Considérons l'entité JPA suivante. Ma classe d'instance d'application doit toujours avoir une référence OneToOne à 4 instances spéciales d'Envelope mais elle possède également un jeu d'enveloppes définies par l'utilisateur 0-infini. Est-ce seulement possible? Est-ce possible avec des références unidirectionnelles et/ou bidirectionnelles?Entité JPA associée à OneToOne et OneToMany

@Entity(name = "Application_Instance") 
public class ApplicationInstance implements Serializable { 

    @Id 
    private int databaseId; 
    private Envelope accountTransfersEnvelope = new Envelope("Account Transfers"); 
    @OneToOne 
    private Envelope newTransationsEnvelope = new Envelope("New Transactions"); 
    @OneToOne 
    private Envelope incomeEnvelope = new Envelope("Income Envelope"); 
    @OneToOne 
    private Envelope creditCarEnvelope= new Envelope("Credit Card"); 
    @OneToMany 
    protected Set<Envelope> userEnvelopes = new HashSet<Envelope>(); 

//rest of class 
} 

Répondre

2

Vous pouvez le faire avec un mappage de table de jointure:

@OneToMany 
@JoinTable(name = "USER_ENVELOPE", 
      joinColumns = { @JoinColumn(name = "APP_ID") }, 
      inverseJoinColumns { @JoinColumn(name = "ENVELOP_ID") })   
protected Set<Envelope> userEnvelopes = new HashSet<Envelope>(); 
Questions connexes