Pergunta

Considere o seguinte entidade JPA. Minha classe instância do aplicativo deve ter sempre uma referência OneToOne a 4 casos especiais de envelope, mas também tem um conjunto de envelopes definidos pelo usuário 0-infinito. Isto é mesmo possível? É possível tanto com unidirecional e / ou referências bidirecionais?

    @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
}
Foi útil?

Solução

Você pode fazer isso com um mapeamento de tabela de junção:

@OneToMany
@JoinTable( name = "USER_ENVELOPE",
            joinColumns = { @JoinColumn( name = "APP_ID" ) },
            inverseJoinColumns { @JoinColumn( name = "ENVELOP_ID" ) } )        
protected Set<Envelope> userEnvelopes = new HashSet<Envelope>();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top