Hi,
I want to use a foreign key as a primary key.
The problem is my foreign key is contained in an object so I don't know how to use Annotations properly to tell Hibernate about this.
My classes:Code:
@Entity
public class Destinatario implements Serializable {
private Long id;
private String nome;
private Long ultimaRequisicao;
@Id
@GeneratedValue
public Long getId() {
return id;
}
...
}
Code:
@Entity
public class Agente implements Serializable {
private Long id;
private String nome;
@Id
@GeneratedValue
public Long getId() {
return id;
}
...
}
Code:
@Entity
public class Requisicao implements Serializable {
private Long numeroRequisicao;
private char tipoRequisicao;
private String conteudoRequisicao;
private Agente agente;
private Destinatario destinatario;
@Id
public Long getNumeroRequisicao() {
return numeroRequisicao;
}
@ManyToOne
public Agente getAgente() {
return agente;
}
@ManyToOne
public Destinatario getDestinatario() {
return destinatario;
}
}
My tables as created by Hibernate:Code:
create table Agente (
id bigint not null auto_increment,
nome varchar(255),
primary key (id)
)
create table Destinatario (
id bigint not null auto_increment,
nome varchar(255),
ultima_requisicao bigint,
primary key (id)
)
create table Requisicao (
numeroRequisicao bigint not null,
conteudoRequisicao varchar(255),
tipoRequisicao char(1) not null,
agente_id bigint,
destinatario_id bigint,
primary key (numeroRequisicao)
)
alter table Requisicao
add index FK7FD68D6919825D20 (agente_id),
add constraint FK7FD68D6919825D20
foreign key (agente_id) references Agente (id)
alter table Requisicao
add index FK7FD68D691AF5B00 (destinatario_id),
add constraint FK7FD68D691AF5B00
foreign key (destinatario_id) references Destinatario (id)
As you can see, there are two foreign keys in my "Requisicao" table.
One is agente_id, other is destinatario_id.
I want the foreign key "destinatario_id" to be the primary key of the "Requisicao" table together with "numeroRequisicao".
Any help will be much appreciated!
Thanks,
Irshad