Diversas chaves estrangeiras JPA

4 respostas
bruno.bariotti

Olá galera.

Estou utilizando Netbeans 7, EclipseLink e JavaDB.

Tenho a seguinte classe SellItem:

@Entity
@Table
public class SellItem implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    @Basic(optional=false)
    private Integer id;
    
    @JoinColumn(name="fk_sell")
    @NotNull
    @ManyToOne(optional=false)
    @Valid
    @Id
    private Sell sell;
    
    @JoinColumn(name="fk_product")
    @ManyToOne(optional=false)
    @Valid
    @Id
    private Product product;
    
    @Column(nullable=false)
    @Min(1)
    private int qnt;

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public int getQnt() {
        return qnt;
    }

    public void setQnt(int qnt) {
        this.qnt = qnt;
    }

    public Sell getSell() {
        return sell;
    }

    public void setSell(Sell sell) {
        this.sell = sell;
    }
    
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

Quando executo minha aplicação, o seguinte erro acontece:

Exception Description: The @JoinColumns on the annotated element [field addressToSend] from the entity class [class br.com.devmedia.entity.Sell] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.

O erro ocorre devido as chaves estrangeiras na classe SellItem.
Alguem sabe como devo proceder?

Obrigado!

4 Respostas

romarcio

Parece que o problema está na classe Sell e não SellItem: [field addressToSend] from the entity class [class br.com.devmedia.entity.Sell]

bruno.bariotti

Segue classe Sell:

@Entity
@Table
public class Sell implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    @Basic(optional=false)
    private Integer id;
    
    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(nullable=false)
    @NotNull
    private Date date;
    
    @Enumerated(EnumType.STRING)
    @Column(nullable=false)
    @NotEmpty
    @NotNull
    private StatusSellType status = StatusSellType.START;
    
    
    @ManyToOne(optional=false)
    @JoinColumn(name="fk_user")
    @NotNull
    @Valid
    @Id
    private User userOf;
    
    @Column(nullable=false)
    @Min(0)
    @NotNull
    private BigDecimal total;
    
    @Column(nullable=false)
    @Min(1)
    @NotNull
    private int times;
    
    
    @ManyToOne(optional=false)
    @JoinColumn(name="fk_address")
    @Valid
    @Id
    private Address addressToSend;
    
    
    @ManyToOne(optional=false)
    @JoinColumn(name="fk_creditcard")
    @Valid
    @Id
    private CreditCard creditcard;
    
    @OneToMany(cascade=CascadeType.ALL,mappedBy="sell")
    private List<SellItem> sellItens = new LinkedList<SellItem>();
}

Não consigo identificar o problema.

romarcio

Parece que você deve declarar todas as colunas no Join.
Algo tipo assim: http://stackoverflow.com/questions/2728011/jpa-onetomany-and-composite-pk

R

Você não pode ter mais de uma anotação @Id em cada classe. Essas chaves estrangeiras não devem ter esta anotação.

Criado 10 de setembro de 2011
Ultima resposta 11 de set. de 2011
Respostas 4
Participantes 3