这里是我的表: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:上面的代码解决了我原来的问题
听起来像一个外键问题。是否有关于外键名称的约定/限制? (我不知道冬眠) – m02ph3u5
你想在Jugador有一个Map。但是你还没有告诉Hibernate地图的关键应该是什么。这是一个整数,但来自哪里? –
我已更新我的问题。地图的关键是Accion的ID(数据是地图中每个条目的值) – gutiory