繁体   English   中英

休眠丢失@OneToMany和@ManyToOne双向关联中的列错误

[英]Hibernate missing column error in @OneToMany and @ManyToOne bidirectional association

这是我的桌子:

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;
    }

休眠日志显示:

初始SessionFactory创建失败.org.hibernate.HibernateException:缺少列:HBAssistant.Acciones中的accionesJugador_KEY

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

已解决:上面的代码解决了我原来的问题

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

您需要给JPA密钥:

@MapKey

如果密钥ID不是主密钥字段

@MapKey(name = "something")

在您的代码中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM