2015-08-25 55 views
1

这里是我的表:Hibernate的缺失列错误

CREATE TABLE IF NOT EXISTS `Jugadores` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '', 
    `nombre` VARCHAR(45) CHARACTER SET 'utf8' COLLATE 'utf8_spanish_ci' NOT NULL COMMENT '', 
    `apellidos` VARCHAR(45) CHARACTER SET 'utf8' COLLATE 'utf8_spanish_ci' NOT NULL COMMENT '', 
    PRIMARY KEY (`id`) COMMENT '' 
) 


CREATE TABLE IF NOT EXISTS `Acciones` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '', 
    `accion` INT UNSIGNED NOT NULL COMMENT '', 
    `idJugador` INT UNSIGNED NOT NULL COMMENT '', 
    PRIMARY KEY (`id`) COMMENT '', 
    CONSTRAINT `fk_acciones_jugadores_id` 
    FOREIGN KEY (`idJugador`) 
    REFERENCES `Jugadores` (`id`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 

这里是我的Java类:

Jugador:

@Entity 
@Table(name ="Jugadores")  
public class Jugador{ 

     private int id; 
     private String nombre; 
     private String apellidos; 

     private Map<Integer, Accion> accionesJugador; 


     public Jugador() {} 

     /** 
     * @return the id 
     */ 
     @Override 
     @Id 
     @GeneratedValue 
     public int getId() { 
      return id; 
     } 

     /** 
     * @param id the id to set 
     */ 
     public void setId(int id) { 
      this.id = id; 
     } 

     /** 
     * @return the nombre 
     */ 
     public String getNombre() { 
      return nombre; 
     } 

     /** 
     * @param nombre the nombre to set 
     */ 
     public void setNombre(String nombre) { 
      this.nombre = nombre; 
     } 

     /** 
     * @return the apellidos 
     */ 
     public String getApellidos() { 
      return apellidos; 
     } 

     /** 
     * @param apellidos the apellidos to set 
     */ 
     public void setApellidos(String apellidos) { 
      this.apellidos = apellidos; 
     } 

     /** 
     * @return the accionesJugador 
     */ 
     @OneToMany(cascade = CascadeType.ALL, mappedBy = "jugador") 
     @MapKey 
     public Map<Integer, Accion> getAccionesJugador() { 
      return accionesJugador; 
     } 

     /** 
     * @param accionesJugador the accionesJugador to set 
     */ 
     public void setAccionesJugador(Map<Integer, Accion> accionesJugador) { 
      this.accionesJugador = accionesJugador; 
     } 

    } 

Accion:

@Entity 
@Table(name = "Acciones") 
public class Accion { 

    private int id; 
    private int accion; 
    private Jugador jugador; 
    public Accion(){}; 

    @Override 
    @Id 
    @GeneratedValue 
    public int getId() { 
     return id; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(int id) { 
     this.id = id; 
    } 

    /** 
    * @return the accion 
    */ 
    public int getAccion() { 
     return accion; 
    } 

    /** 
    * @param accion the accion to set 
    */ 
    public void setAccion(int accion) { 
     this.accion = accion; 
    } 

    /** 
    * @return the jugador 
    */ 
    @ManyToOne() 
    @JoinColumn(name = "idJugador") 
    public Jugador getJugador() { 
     return jugador; 
    } 

    /** 
    * @param jugador the jugador to set 
    */ 
    public void setJugador(Jugador jugador) { 
     this.jugador = jugador; 
    } 

和Hibernate的日志说:

Initial SessionFactory creation failed.org.hibernate.HibernateException: Missing column: accionesJugador_KEY in HBAssistant.Acciones

编辑:Map<Integer, Accion> accionesJugador的关键是的 Accion

解决了这个ID:上面的代码解决了我原来的问题

+0

听起来像一个外键问题。是否有关于外键名称的约定/限制? (我不知道冬眠) – m02ph3u5

+0

你想在Jugador有一个Map 。但是你还没有告诉Hibernate地图的关键应该是什么。这是一个整数,但来自哪里? –

+0

我已更新我的问题。地图的关键是Accion的ID(数据是地图中每个条目的值) – gutiory

回答

1

您在@OneToMany关系中使用地图。

你需要给JPA的关键:

@MapKey 

如果密钥ID不是主键字段

@MapKey(name = "something") 

在你的代码:

 @OneToMany(cascade = CascadeType.ALL, mappedBy = "jugador") 
     @MapKey 
     public Map<Integer, Accion> getAccionesJugador() { 
      return accionesJugador; 
     } 
+0

我做了你的建议,并且出现了另一个错误:创建初始SessionFactory failed.org.hibernate.HibernateException:缺少列: acciones_KEY in HBAssistant.Acciones – gutiory

+0

其中是您的类Accion中字段“id”的声明?你在这里复制/粘贴错过了什么? – Pras

+0

我已更新我的问题。对不起,我错了复制/粘贴。 – gutiory