-->
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.  [ 1 post ] 
Author Message
 Post subject: Optional foreign key
PostPosted: Sat Apr 23, 2005 4:33 pm 
Newbie

Joined: Mon Apr 18, 2005 11:12 am
Posts: 4
Hibernate version:
3.0.1

Problem:
Is possible to exist an optional foreign-key in an Hibernate Mapping?
This is my scenario:
All my mapping have composite-id like
Code:
@Embeddable
public class OrgaoPk implements Comparable, Serializable {

   private String competencia;
   private int codigo;
   
   public OrgaoPk() {}
   
   public OrgaoPk(String competencia, int codigo) {
      this.competencia = competencia;
      this.codigo = codigo;
   }

   @Column(name = "fk_competencia", nullable = false, length = 6)
   public String getCompetencia() {
      return competencia;
   }
   (...)
   @Column(name = "codigo", nullable = false)
   public int getCodigo() {
      return codigo;
   }
   (...)
}


Code:
@Entity
@Table(name = "tb_orgao")
public class Orgao implements Comparable, Serializable {

   private OrgaoPk pk;
   private Servidor titular;

   @Id(generate = GeneratorType.NONE)
   public OrgaoPk getPk() {
      return pk;
   }
   (...)
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumns({
      @JoinColumn(name = "fk_competencia", referencedColumnName = "fk_competencia", insertable = false, updatable = false, nullable = false, primaryKey = true),
      @JoinColumn(name = "fk_servidor_titular", referencedColumnName = "matricula", insertable = false, updatable = false, nullable = false, primaryKey = true)
   })
   public Servidor getTitular() {
      return titular;
   }


It occurs that field fk_servidor_titular can be null, but never fk_competencia
And when this happens is generated an exception, therefore it does not exist in the tb_servidor table.


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

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.