简体   繁体   中英

Hibernate missing column error in @OneToMany and @ManyToOne bidirectional association

Here are my tables:

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,

)

And here are my Java Classes:

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

And the Hibernate log says:

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

EDIT: The key in the Map<Integer, Accion> accionesJugador is the id of the Accion

SOLVED: The code above resolves my original problem

you are using a map in your @OneToMany relationship.

you need to give JPA the key :

@MapKey

if the key id not the primary key field

@MapKey(name = "something")

in your code :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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