These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: [SOLVED] Using foreign key as primary key
PostPosted: Sat May 15, 2010 10:31 am 
Newbie

Joined: Fri May 14, 2010 5:51 pm
Posts: 8
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


Last edited by irs86 on Mon May 17, 2010 12:15 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Using foreign key as primary key
PostPosted: Mon May 17, 2010 12:14 pm 
Newbie

Joined: Fri May 14, 2010 5:51 pm
Posts: 8
I have found the solution for my problem.

If anyone have the same issue, follow:
http://java-aap.blogspot.com/2006/04/hibernate-annotations-composite.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.