-->
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]Composite foreign key with Temporal field
PostPosted: Sun Aug 23, 2009 8:30 am 
Newbie

Joined: Sun Aug 23, 2009 8:09 am
Posts: 9
Hi everyone. I have a problem with one entity referencing a composite foreign key of another entity. I have read the chapters about it on the Manning and have googled. Hibernate accepts the initial annotations, but throws an Exceptions when trying to use the Dao to create the entity referencing the other one.


JDBC exception on Hibernate data access: SQLException for SQL [/* insert es.udc.madtyn.gimnasio.model.incidencia.Incidencia */ insert into Incidencia (descripcion, fecha, nombre, idSala) values (?, ?, ?, ?)]; SQL state [HY000]; error code [1364]; could not insert: [es.udc.madtyn.gimnasio.model.incidencia.Incidencia]; nested exception is org.hibernate.exception.GenericJDBCException: could not insert: [es.udc.madtyn.gimnasio.model.incidencia.Incidencia]

java.sql.SQLException: Field 'tvi' doesn't have a default value



Could it be because tvi is Calendar? How do I mend it?

Incidencia references EstEmp with composite key EstEmpId. My code:

Code:
                           /* Tthe object containing the composite key of EstEmp */

@Embeddable
public class EstEmpId implements Serializable {
   
   private Empleado empleado;
   private Calendar tvi;
   
   public EstEmpId() {}
   
   public EstEmpId(Empleado empleado, Calendar tvi) {
      this.empleado = empleado;
      this.tvi = tvi;
   }

   @ManyToOne(optional=false, fetch=FetchType.EAGER)
   @JoinColumn(name="idEmp")
   public Empleado getEmpleado() {
      return empleado;
   }
   public void setEmpleado(Empleado empleado) {
      this.empleado = empleado;
   }
   public Calendar getTvi() {
      return tvi;
   }
   public void setTvi(Calendar tvi) {
      this.tvi = tvi;
   }

}

          /* EstEmp, the referenced object */

@Entity
public class EstEmp {
   
   private EstEmpId id;
   private Calendar tvf;
   private String numCuenta;
   private String domicilio;
   private Double sueldo;
   private Puesto puesto;
   
   public EstEmp() {}
   
   public EstEmp(EstEmpId eei, Calendar tvf, String numCuenta,
         String domicilio, Double sueldo, Puesto puesto) {
      super();
      this.id = eei;
      this.tvf = tvf;
      this.numCuenta = numCuenta;
      this.domicilio = domicilio;
      if(sueldo != null)   this.sueldo = sueldo;
      else            this.sueldo = 0.0;
      this.puesto = puesto;
   }

   @Id
   public EstEmpId getId() {
      return id;
   }

   @ManyToOne(fetch=FetchType.EAGER)
   @JoinColumn(name="idPuesto")
   public Puesto getPuesto() {
      return puesto;
   }
   
   public String getNumCuenta() {
      return numCuenta;
   }

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

         /* Rest of getters/setters down here */
}

      /* Incidencia, the class wichs references to the composite foreign key */

@Entity
public class Incidencia {

   private Long numIncidencia;
   private String nombre;
   private Calendar fecha;
   private String desc;
   private EstEmp empAlta, empBaja;
   private Sala sala;

   public Incidencia() {}

   public Incidencia(String nombre, Calendar fecha, String desc,
         EstEmp empAlta, Sala sala) {
      super();
      this.nombre = nombre;
      this.fecha = fecha;
      this.desc = desc;
      this.empAlta = empAlta;
      this.sala = sala;
   }

   @Column(name = "idIncidencia")
   @SequenceGenerator(name = "idIncidenciaGenerator", sequenceName = "IncidenciaSeq")
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO, generator = "idIncidenciaGenerator")
   public Long getNumIncidencia() {
      return numIncidencia;
   }

   @ManyToOne(optional=false, fetch=FetchType.EAGER)
   @JoinColumns({
      @JoinColumn(name="tvi", insertable=false, updatable=false, referencedColumnName="tvi"),
      @JoinColumn(name="idEmpAlta", insertable=false, updatable=false, referencedColumnName="idEmp")
         
   })
   public EstEmp getEmpAlta() {
      return empAlta;
   }

   @ManyToOne(optional=true, fetch=FetchType.EAGER)
   @JoinColumns({
      @JoinColumn(name="tvi", insertable=false, updatable=false, referencedColumnName="tvi"),
      @JoinColumn(name="idEmpBaja", insertable=false, updatable=false, referencedColumnName="idEmp")
         
   })
   public EstEmp getEmpBaja() {
      return empBaja;
   }
         /* Rest of getters/setters down here */
              ............
}



Thank you very much.


Last edited by madtyn on Mon Aug 24, 2009 7:27 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: [SOLVED]Composite foreign key with Temporal field
PostPosted: Mon Aug 24, 2009 7:25 am 
Newbie

Joined: Sun Aug 23, 2009 8:09 am
Posts: 9
I mended all by myself. If someone has a similar problem, the code is down here.

I don't know if all changes were necessary, because I did all and after that proved the web app, but they sure are correct for my design so I'm not removing any of them. The changes are:

- I changed the getters and setters for the calendar field so they could get and set as if it were a Date field, but the precission of Calendar is working down there.

- I added the @Temporal annotation because of a Date field being part of the composite key of EstEmp.

- I used the @AssociationOverrides anottation for describing the @ManyToOne relation from the Incidencia to the EstEmp.

- The code over the getter of the EstEmp Object with composite key contained in Incidencia is almost the same, I needed to change something in the database because it wasn't prepared for having two relations with two EstEmp objects, but for the rest is similar.

- I used the @AttributeOverrides annotation for describing the mapping of part of the foreign key referencing the Date field of the foreign composite key. Maybe this is the unnecesary part and could be removed, because this is told too in the JoinColumns, but I'm not sure.

- I used as well @AttributeOverrides over the Id composite key field in EstEmp for describing the composite key, as the Manning book of Hibernate says.


Finally, the code:

Code:

                                  /* The object containing the composite key of EstEmp */

@Embeddable
public class EstEmpId implements Serializable {
   
   private Empleado empleado;
   private Calendar tvi=Calendar.getInstance();
   
   public EstEmpId() {}
   
   public EstEmpId(Empleado empleado, Calendar tvi) {
      this.empleado = empleado;
      if(tvi!=null) this.tvi=tvi;
   }
   
   public EstEmpId(Empleado empleado, Date tvi) {
      this.empleado = empleado;
      if(tvi!=null)   setTvi(tvi);
   }

   @ManyToOne(optional=false, fetch=FetchType.EAGER)
   @JoinColumn(name="idEmp")
   public Empleado getEmpleado() {
      return empleado;
   }
   public void setEmpleado(Empleado empleado) {
      this.empleado = empleado;
   }
   
   @Temporal(TemporalType.DATE)
   public Date getTvi() {
      return tvi.getTime();
   }
   public void setTvi(Date tvi) {
      javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), "tvi="+tvi);
      this.tvi.setTime(tvi);
   }
}

                                               /* EstEmp, the referenced object */

@Entity
public class EstEmp {
   
   private EstEmpId id;
   private Calendar tvf;
   private String numCuenta;
   private String domicilio;
   private Double sueldo;
   private Puesto puesto;
   
   public EstEmp() {}
   
   public EstEmp(EstEmpId eei, Calendar tvf, String numCuenta,
         String domicilio, Double sueldo, Puesto puesto) {
      super();
      this.id = eei;
      this.tvf = tvf;
      this.numCuenta = numCuenta;
      this.domicilio = domicilio;
      if(sueldo != null)   this.sueldo = sueldo;
      else            this.sueldo = 0.0;
      this.puesto = puesto;
   }

   @Id
   @AttributeOverrides({
       @AttributeOverride(name="idEmp", column= @Column(name="idEmp")),
       @AttributeOverride(name="tvi", column= @Column(name="tvi"))
   })
   public EstEmpId getId() {
      return id;
   }

   @ManyToOne(fetch=FetchType.EAGER)
   @JoinColumn(name="idPuesto")
   public Puesto getPuesto() {
      return puesto;
   }

   @Temporal(TemporalType.DATE)
   public Date getTvf() {
      if(tvf!=null)   return tvf.getTime();
      else         return null;
   }

   public String getNumCuenta() {
      return numCuenta;
   }

   public String getDomicilio() {
      return domicilio;
   }

   public Double getSueldo() {
      return sueldo;
   }

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

   public void setTvf(Date tvf) {
      if(tvf!=null) {
         Calendar c = Calendar.getInstance();
         c.setTime(tvf);
         this.tvf = c;
      }
      else this.tvf = null;
   }
}

                      /* Incidencia, the class wichs references to the composite foreign key */


@Entity
@AssociationOverrides({
   @AssociationOverride(name="empAlta.id",
                   joinColumns= {
                        @JoinColumn(name="idEmpAlta", referencedColumnName="idEmp"),
                        @JoinColumn(name="tviAlta", referencedColumnName="tvi")
                  }),
   @AssociationOverride(name="empBaja.id",
                   joinColumns= {
                        @JoinColumn(name="idEmpBaja", referencedColumnName="idEmp"),
                        @JoinColumn(name="tviBaja", referencedColumnName="tvi")
                  })
})
@AttributeOverrides({
       @AttributeOverride(name="tviAlta", column= @Column(name="tviAlta")),
       @AttributeOverride(name="tviBaja", column= @Column(name="tviBaja"))
   })
public class Incidencia {

   private Long numIncidencia;
   private String nombre;
   private Calendar fecha;
   private String desc;
   private EstEmp empAlta, empBaja;
   private Sala sala;

   public Incidencia() {}

   public Incidencia(String nombre, Calendar fecha, String desc,
         EstEmp empAlta, Sala sala) {
      super();
      this.nombre = nombre;
      this.fecha = fecha;
      this.desc = desc;
      this.empAlta = empAlta;
      this.sala = sala;
   }

   @Column(name = "idIncidencia")
   @SequenceGenerator(name = "idIncidenciaGenerator", sequenceName = "IncidenciaSeq")
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO, generator = "idIncidenciaGenerator")
   public Long getNumIncidencia() {
      return numIncidencia;
   }

   @ManyToOne(optional=false, fetch=FetchType.EAGER)
   @JoinColumn(name="idSala")
   public Sala getSala() {
      return sala;
   }

   @ManyToOne(optional=false, fetch=FetchType.EAGER)
   @JoinColumns({
      @JoinColumn(name="tviAlta", insertable=true, updatable=false, referencedColumnName="tvi"),
      @JoinColumn(name="idEmpAlta", insertable=true, updatable=false, referencedColumnName="idEmp")
         
   })
   public EstEmp getEmpAlta() {
      return empAlta;
   }

   @ManyToOne(optional=true, fetch=FetchType.EAGER)
   @JoinColumns({
      @JoinColumn(name="tviBaja", insertable=false, updatable=true, referencedColumnName="tvi"),
      @JoinColumn(name="idEmpBaja", insertable=false, updatable=true, referencedColumnName="idEmp")
         
   })
   public EstEmp getEmpBaja() {
      return empBaja;
   }

   public String getNombre() {
      return nombre;
   }

   @Column(name="fecha")
   public Date getFecha() {
      return fecha.getTime();
   }
   
   @Column(name="descripcion")
   public String getDesc() {
      return desc;
   }
   
   @Transient
   public String getDate() {
      return DateHandler.dateToString(fecha.getTime());
   }
                              /* Rest of getters/setters down here */
}


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.