Reputation: 397
This is my Character class:
package webgame.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "character")
public class Character {
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@Column(name = "nivel")
private int nivel;
@Column(name = "experiencia")
private int experiencia;
@Column(name = "fuerza")
private float fuerza;
@Column(name = "inteligencia")
private float inteligencia;
@Column(name = "aguante")
private float aguante;
@Column(name = "agilidad")
private float agilidad;
@Column(name = "suerte")
private float suerte;
@Column(name = "dinero")
private float dinero;
public Character() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNivel() {
return nivel;
}
public void setNivel(int nivel) {
this.nivel = nivel;
}
public int getExperiencia() {
return experiencia;
}
public void setExperiencia(int experiencia) {
this.experiencia = experiencia;
}
public float getFuerza() {
return fuerza;
}
public void setFuerza(float fuerza) {
this.fuerza = fuerza;
}
public float getInteligencia() {
return inteligencia;
}
public void setInteligencia(float inteligencia) {
this.inteligencia = inteligencia;
}
public float getAguante() {
return aguante;
}
public void setAguante(float aguante) {
this.aguante = aguante;
}
public float getAgilidad() {
return agilidad;
}
public void setAgilidad(float agilidad) {
this.agilidad = agilidad;
}
public float getSuerte() {
return suerte;
}
public void setSuerte(float suerte) {
this.suerte = suerte;
}
public float getDinero() {
return dinero;
}
public void setDinero(float dinero) {
this.dinero = dinero;
}
}
And my BD:
Im using Spring, and when i enter to the path "/" i execute the insert, but i am geting:
SQL Error: 1064, SQLState: 42000
2015-03-26 13:09:20.050 ERROR 5764 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'character (agilidad, aguante, dinero, experiencia, fuerza, inteligencia, nivel, ' at line 1
This is how i call the insert:
@RequestMapping(value="/", method = RequestMethod.GET)
public String indexForm(Model model) {
Character c = new Character();
c.setNivel(1);
c.setExperiencia(2);
c.setFuerza(5);
c.setInteligencia(10);
c.setAguante(5);
c.setAgilidad(3);
c.setSuerte(2);
c.setDinero(100);
Session session = HibernateUtil.getSession();
session.beginTransaction();
session.save(c);
session.getTransaction().commit();
session.close();
return "index";
}
I have search and i dont think i have a sql reserved keyword.
Upvotes: 1
Views: 1288
Reputation: 409
Change your table name and coloumn names. Ex: Change 'Nombre' to 'nombre' . (Remove Capital letter in all columns).
Upvotes: 0
Reputation: 125925
CHARACTER
is a reserved word. If you're going to name SQL objects with such words, then you must ensure that they are appropriately quoted.
Adapting Pascal Thivent's excellent answer to Creating field with reserved word name with JPA:
With Hibernate as JPA 1.0 provider, you can escape a reserved keyword by enclosing it within backticks:
@Table(name = "`character`")
This is the syntax inherited from Hiberate Core:
5.4. SQL quoted identifiers
You can force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the mapping document. Hibernate will use the correct quotation style for the SQL Dialect. This is usually double quotes, but the SQL Server uses brackets and MySQL uses backticks.
<class name="LineItem" table="`Line Item`"> <id name="id" column="`Item Id`"/><generator class="assigned"/></id> <property name="itemNumber" column="`Item #`"/> ... </class>
In JPA 2.0, the syntax is standardized and becomes:
@Table(name="\"character\"")
Upvotes: 2