gpt4 book ai didi

mysql - SQL 错误 : 1064 Hibernate

转载 作者:行者123 更新时间:2023-11-29 06:28:46 25 4
gpt4 key购买 nike

这是我的角色类:

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

还有我的业务发展: enter image description here

我使用 Spring,当我进入路径“/”时,我执行插入,但我得到:

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

这就是我所说的插入:

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

我有搜索,但我认为我没有 sql 保留关键字。

最佳答案

CHARACTERreserved word .如果您打算用这样的词命名 SQL 对象,那么您必须确保它们被适本地引用。

适应Pascal Thivent's excellent answerCreating field with reserved word name with JPA :

使用 Hibernate 作为 JPA 1.0 提供程序,您可以通过将保留关键字括在反引号中来转义保留关键字:

@Table(name = "`character`")

这是继承自 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>

在 JPA 2.0 中,语法被标准化并变为:

@Table(name="\"character\"")

引用资料

相关问题

关于mysql - SQL 错误 : 1064 Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29278251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com