GrayFox
GrayFox

Reputation: 13

org.hibernate.MappingException: Foreign key (FK12A711396456CA10:) must have same number of columns as the referenced primary key

I'm having this error at Netbeans in my Java code:

org.hibernate.MappingException: Foreign key (FK12A711396456CA10:devolucion_master [devolucion_consecutivo])) must have same number of columns as the referenced primary key (devolucion [detalle_ticket_id,detalle_ticket_ticket_id,detalle_ticket_fondo_fijo_id,detalle_ticket_caja_id,consecutivo] 

I made a foreign key from DevolucionMaster to Devolucion, using "consecutivo" from Devolucion to my variable "consecutivo" for DevolucionMaster, the problem is that for Devolucion the "key" is composite key, and I only use for the foreign key one element of the key, maybe that's why (that it needs to be used the 5 that makes the primary key).

Here's the DevolucionMaster.hbm.mxl:

<hibernate-mapping>
  <class catalog="pos" name="dunosusa.pos.model.DevolucionMaster" table="devolucion_master">
    <composite-id class="dunosusa.pos.model.DevolucionMasterId" name="id">
      <key-property name="id" type="int">
        <column name="id"/>
      </key-property>
      <key-property name="detalleTicketTicketId" type="int">
        <column name="detalle_ticket_ticket_id"/>
      </key-property>
      <key-property name="detalleTicketFondoFijoId" type="int">
        <column name="detalle_ticket_fondo_fijo_id"/>
      </key-property>
      <key-property name="detalleTicketCajaId" type="int">
        <column name="detalle_ticket_caja_id"/>
      </key-property>
    </composite-id>
    <many-to-one class="dunosusa.pos.model.Devolucion" fetch="select" name ="devolucion">
      <column name="devolucion_consecutivo" not-null="true"/>
    </many-to-one>
    <many-to-one class="dunosusa.pos.model.Usuario" fetch="select" name="usuario">
      <column length="6" name="usuario_clave_autorizo"/>
    </many-to-one>
    <many-to-one class="dunosusa.pos.model.Ticket" fetch="select" insert="false" name="ticket" update="false">
      <column name="detalle_ticket_ticket_id" not-null="true"/>
      <column name="detalle_ticket_fondo_fijo_id" not-null="true"/>
      <column name="detalle_ticket_caja_id" not-null="true"/>
    </many-to-one>
    <property name="total" type="big_decimal">
      <column name="total" not-null="true" precision="10"/>
    </property>
    <property name="fecha" type="timestamp">
      <column length="19" name="fecha"/>
    </property>
  </class>
</hibernate-mapping>

here the Devolucion.hbm.xml:

<hibernate-mapping>
  <class catalog="pos" name="dunosusa.pos.model.Devolucion" table="devolucion">
    <composite-id class="dunosusa.pos.model.DevolucionId" name="id">
      <key-property name="detalleTicketId" type="int">
        <column name="detalle_ticket_id"/>
      </key-property>
      <key-property name="detalleTicketTicketId" type="int">
        <column name="detalle_ticket_ticket_id"/>
      </key-property>
      <key-property name="detalleTicketFondoFijoId" type="int">
        <column name="detalle_ticket_fondo_fijo_id"/>
      </key-property>
      <key-property name="detalleTicketCajaId" type="int">
        <column name="detalle_ticket_caja_id"/>
      </key-property>
      <key-property name="consecutivo" type="int">
        <column name="consecutivo"/>
      </key-property>
    </composite-id>
    <many-to-one class="dunosusa.pos.model.MotivoDevolucion" fetch="select" name="motivoDevolucion">
      <column name="motivo_devolucion_id" not-null="true"/>
    </many-to-one>
    <many-to-one class="dunosusa.pos.model.DetalleTicket" fetch="select" insert="false" name="detalleTicket" update="false">
      <column name="detalle_ticket_id" not-null="true"/>
      <column name="detalle_ticket_ticket_id" not-null="true"/>
      <column name="detalle_ticket_fondo_fijo_id" not-null="true"/>
      <column name="detalle_ticket_caja_id" not-null="true"/>
    </many-to-one>
    <many-to-one class="dunosusa.pos.model.ControlCorte" fetch="select" name="controlCorte">
      <column name="control_corte_fondo_fijo_id" not-null="true"/>
      <column name="control_corte_caja_id" not-null="true"/>
    </many-to-one>
    <property name="cantidad" type="big_decimal">
      <column name="cantidad" precision="8"/>
    </property>
    <property name="fecha" type="timestamp">
      <column length="19" name="fecha"/>
    </property>
    <property name="comentario" type="string">
      <column length="150" name="comentario"/>
    </property>
    <property name="controlDevolucion" type="boolean">
      <column name="control_devolucion" not-null="true"/>
    </property>
    <set name="devolucionMasters" inverse="true">
        <key>
            <column name="devolucion_consecutivo" not-null="true" />
        </key>
        <one-to-many class="dunosusa.pos.model.DevolucionMaster" />
    </set>
  </class>
</hibernate-mapping>

DevolucionMaster.java: (only the variables, not the set and get)

public class DevolucionMaster  implements java.io.Serializable {

     private DevolucionMasterId id;
     private Devolucion devolucion;
     private Usuario usuario;
     private Ticket ticket;
     private BigDecimal total;
     private Date fecha;
}

Devolucion.java: (same as for DevolucionMaster)

public class Devolucion  implements java.io.Serializable {

     private DevolucionId id;
     private MotivoDevolucion motivoDevolucion;
     private DetalleTicket detalleTicket;
     private ControlCorte controlCorte;
     private BigDecimal cantidad;
     private Date fecha;
     private String comentario;
     private boolean controlDevolucion;
     private Set devolucionMasters = new HashSet(0);
}

I don't know what's my error, I've searched over internet about similar errors but no one solution I've read has worked (forgive my bad english).

Thanks a lot!

Upvotes: 1

Views: 3926

Answers (1)

Cebence
Cebence

Reputation: 2416

Yes, you have a composite primary key for the Devolucion class, and you are trying to refer to it by just one simple column/field devolucion_master.devolucion_consecutivo.

I don't know of a way to easily tell Hibernate that devolucion_consecutivo column is actually of type dunosusa.pos.model.DevolucionId.

That's why I have never used composite primary keys. I always have one primary key of type Long (bigint) and enforce uniqueness for a composition of foreign keys where it's needed. It's easier to work with, no issues like the one you're having.

APPENDIX: I just did some digging and, actually, there is a way to do it :-) But as I said it's not as easy as using single column PK/FK keys.

Instead of this:

<hibernate-mapping>
  <class catalog="pos" name="dunosusa.pos.model.DevolucionMaster" table="devolucion_master">
  ...
    <many-to-one class="dunosusa.pos.model.Devolucion" fetch="select" name="devolucion">
      <column name="devolucion_consecutivo" not-null="true"/>
    </many-to-one>
    ...

You should list all PK columns in the FK like this:

<hibernate-mapping package="dunosusa.pos.model">
  <class catalog="pos" name="DevolucionMaster" table="devolucion_master">
    ...
    <many-to-one class="Devolucion" fetch="select" name="devolucion">
      <column name="devolucion_detalle_ticket_id" not-null="true"/>
      <column name="devolucion_detalle_ticket_ticket_id" not-null="true"/>
      <column name="devolucion_detalle_ticket_fondo_fijo_id" not-null="true"/>
      <column name="devolucion_detalle_ticket_caja_id" not-null="true"/>
      <column name="devolucion_consecutivo" not-null="true"/>
    </many-to-one>
    ...

A little advice - use the package attribute of the hibernate-mapping element so you don't have to type it in everywhere - it makes it more readable.

Good luck.

Upvotes: 1

Related Questions