Hi guys i am having this exception

Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FKF5B10F06F8C1E633:detalle_pedidos [pedido_id])) must have same number of columns as the referenced primary key (pedidos [pedido_id,cliente_id,empleado_id])
Exception in thread "main" java.lang.ExceptionInInitializerError

according to this my table pedidos mismatch detalle_pedidos

here pedidos pojo

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Pedidos generated by hbm2java
 */
public class Pedidos  implements java.io.Serializable {


     private PedidosId id;
     private Empleados empleados;
     private Clientes clientes;
     private Date fechaPedido;
     private Date fechaEntrega;
     private String estado;
     private BigDecimal total;
     private Set detallePedidoses = new HashSet(0);

    public Pedidos() {
    }

	
    public Pedidos(PedidosId id, Empleados empleados, Clientes clientes, Date fechaPedido, Date fechaEntrega, String estado, BigDecimal total) {
        this.id = id;
        this.empleados = empleados;
        this.clientes = clientes;
        this.fechaPedido = fechaPedido;
        this.fechaEntrega = fechaEntrega;
        this.estado = estado;
        this.total = total;
    }
    public Pedidos(PedidosId id, Empleados empleados, Clientes clientes, Date fechaPedido, Date fechaEntrega, String estado, BigDecimal total, Set detallePedidoses) {
       this.id = id;
       this.empleados = empleados;
       this.clientes = clientes;
       this.fechaPedido = fechaPedido;
       this.fechaEntrega = fechaEntrega;
       this.estado = estado;
       this.total = total;
       this.detallePedidoses = detallePedidoses;
    }
   
    public PedidosId getId() {
        return this.id;
    }
    
    public void setId(PedidosId id) {
        this.id = id;
    }
    public Empleados getEmpleados() {
        return this.empleados;
    }
    
    public void setEmpleados(Empleados empleados) {
        this.empleados = empleados;
    }
    public Clientes getClientes() {
        return this.clientes;
    }
    
    public void setClientes(Clientes clientes) {
        this.clientes = clientes;
    }
    public Date getFechaPedido() {
        return this.fechaPedido;
    }
    
    public void setFechaPedido(Date fechaPedido) {
        this.fechaPedido = fechaPedido;
    }
    public Date getFechaEntrega() {
        return this.fechaEntrega;
    }
    
    public void setFechaEntrega(Date fechaEntrega) {
        this.fechaEntrega = fechaEntrega;
    }
    public String getEstado() {
        return this.estado;
    }
    
    public void setEstado(String estado) {
        this.estado = estado;
    }
    public BigDecimal getTotal() {
        return this.total;
    }
    
    public void setTotal(BigDecimal total) {
        this.total = total;
    }
    public Set getDetallePedidoses() {
        return this.detallePedidoses;
    }
    
    public void setDetallePedidoses(Set detallePedidoses) {
        this.detallePedidoses = detallePedidoses;
    }




}

and here my pojo Detalle_pedidos

/**
 * DetallePedidos generated by hbm2java
 */
public class DetallePedidos  implements java.io.Serializable {


     private DetallePedidosId id;
     private Productos productos;
     private Pedidos pedidos;
     private short cantidad;

    public DetallePedidos() {
    }

    public DetallePedidos(DetallePedidosId id, Productos productos, Pedidos pedidos, short cantidad) {
       this.id = id;
       this.productos = productos;
       this.pedidos = pedidos;
       this.cantidad = cantidad;
    }
   
    public DetallePedidosId getId() {
        return this.id;
    }
    
    public void setId(DetallePedidosId id) {
        this.id = id;
    }
    public Productos getProductos() {
        return this.productos;
    }
    
    public void setProductos(Productos productos) {
        this.productos = productos;
    }
    public Pedidos getPedidos() {
        return this.pedidos;
    }
    
    public void setPedidos(Pedidos pedidos) {
        this.pedidos = pedidos;
    }
    public short getCantidad() {
        return this.cantidad;
    }
    
    public void setCantidad(short cantidad) {
        this.cantidad = cantidad;
    }




}


I cant see any problems  any idea? i am really new to hibernate

Dani AI

Generated

As suggested, the problem is a mapping contradiction: the parent entity uses a composite primary key (an Id object with multiple columns) but the child mapping only references a single column. Hibernate requires a foreign key to match the full primary key it points to, so the child must include all PK columns (or the parent must be changed to a single-column key).

Two practical fixes:

  • Keep the existing DB keys and change the DetallePedidos mapping so its composite-id includes the same three order columns (pedido_id, cliente_id, empleado_id). In hbm.xml you can use a key-many-to-one inside the composite-id and then a many-to-one (insert="false" update="false") for the association, for example:
<composite-id name="id" class="DetallePedidosId">
  <key-many-to-one name="pedidos" class="Pedidos">
    <column name="pedido_id"/>
    <column name="cliente_id"/>
    <column name="empleado_id"/>
  </key-many-to-one>
  <key-property name="productoId" column="producto_id"/>
</composite-id>

<many-to-one name="pedidos" class="Pedidos" insert="false" update="false">
  <column name="pedido_id"/>
  <column name="cliente_id"/>
  <column name="empleado_id"/>
</many-to-one>
  • Or simplify the model by giving Pedidos a single surrogate PK (e.g., pedido_id only). That makes the DetallePedidos many-to-one trivial and avoids the complexity of composite keys. This is usually the cleaner long-term choice unless your business rules require composite keys.

Quick troubleshooting checklist for :

  1. Inspect the actual DB PK for pedidos (SHOW CREATE TABLE / DESCRIBE).
  2. Inspect DetallePedidosId and ensure it contains the same columns or an embedded PedidosId.
  3. Update mappings (or schema) and regenerate or adapt POJOs.
  4. Rebuild and watch for the same MappingException.

If you post your DetallePedidosId or the mapping file I can point to the exact edits.

It seems that there is an issue with the mapping that you have given for the class. Go through the mapping as to where the contradiction is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.