Boa tarde galera, estou com o seguinte problema:
Tenho as entidades Status, Pedido e StatusPedido:
[code]@Entity
@Table(name = “TB_STATUS”)
public class Status {
@Id
@Column(name = "CD_STATUS")
private Integer id;
@OneToMany(mappedBy = "status")
@JoinColumn(name="CD_PEDIDO")
private List<StatusPedido> statusPedidos = new ArrayList<StatusPedido>();
}
@Entity
@Table(name = “TB_PEDIDO”)
public class Pedido {
@Id
@Column(name = "CD_PEDIDO")
private Long id;
@ManyToOne
private StatusPedido status;
}
@Entity
@Table(name = “TB_STATUS_PEDIDO”)
public class StatusPedido {
@EmbeddedId
private StatusPedidoId id;
@ManyToOne
@JoinColumn(name = "CD_STATUS")
private Status status;
@OneToMany(mappedBy = "status")
private List<Pedido> pedidos = new ArrayList<Pedido>();
}
// A chave composta de StatusPedido
@Embeddable
public class StatusPedidoId {
@Column(name = "CD_STATUS")
private Integer idStatus;
@Column(name = "CD_PEDIDO")
private Long idPedido;
}[/code]Status é um-para-muitos com StatusPedido, que é um-para-muitos com Pedido. Devido à chave composta de StatusPedido, estou tendo problemas. Na forma como está, recebo a seguinte exception:
Exception in thread "main" org.hibernate.AnnotationException: A Foreign key refering br.com.xxxx.StatusPedido from br.com.xxxx.Pedido has the wrong number of column. should be 2
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:276)
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1322)
at org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:655)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:587)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:541)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
Alguém sabe a forma correta de fazer esse mapeamento? Obrigado! :thumbup: