gpt4 book ai didi

java - 无法使用 Hibernate 创建实体。 SQL语法异常

转载 作者:行者123 更新时间:2023-12-01 21:17:36 24 4
gpt4 key购买 nike

我花了一整天的时间来解决这个问题。 session.getTransaction().commit();

在线发生错误
private SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

@Override
public void initGroup(Group group) throws BotException {
Session session = sessionFactory.openSession();

try {
session.beginTransaction();
session.persist(group);
session.getTransaction().commit();
} catch (PersistenceException e)
{
throw new BotException("You are already registred");
}
finally {
session.close();
sessionFactory.close();
}

令人惊讶的是,我对“Station”实体具有相同的功能,但它工作正常

真是太奇怪了。不知道怎么解决。

这是我的 Group.class

@Entity
@Table(name = "group")
public class Group {
private String name;
private String password;
private String telegramId;
private int experience;
private int money;
private String nowStation;

public Group() {}

public Group(String name, String password, String telegramId) {
this.name = name;
this.password = password;
this.telegramId = telegramId;
}

@Id
@Column(name = "name", nullable = false, length = 45)
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Basic
@Column(name = "password", nullable = false, length = 45)
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Basic
@Column(name = "telegram_id", nullable = false, length = 45)
public String getTelegramId() {
return telegramId;
}

public void setTelegramId(String telegramId) {
this.telegramId = telegramId;
}

@Basic
@Column(name = "experience", nullable = true)
public int getExperience() {
return experience;
}

public void setExperience(int experience) {
this.experience = experience;
}

@Basic
@Column(name = "money", nullable = true)
public int getMoney() {
return money;
}

public void setMoney(int money) {
this.money = money;
}

@Basic
@Column(name = "now_station", nullable = true, length = 45)
public String getNowStation() {
return nowStation;
}

public void setNowStation(String nowStation) {
this.nowStation = nowStation;
}

和 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/game_data_base</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.username">root</property>
<property name="connection.password">*******</property>
<mapping class="model.Group"/>
<mapping class="model.Station"/>
<mapping class="model.User"/>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>

错误

сен 25, 2016 7:42:54 PM org.telegram.telegrambots.logging.BotLogger severe
19:42:54.374 [PMPUTestBot Telegram Executor] DEBUG org.hibernate.service.internal.AbstractServiceRegistryImpl - Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries
SEVERE: BOTSESSION
19:42:54.374 [PMPUTestBot Telegram Executor] INFO org.hibernate.orm.connections.pooling - HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/game_data_base]
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
19:42:54.374 [PMPUTestBot Telegram Executor] DEBUG org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl - Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1411)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:475)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3168)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2382)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
at dao.GroupDaoImpl.initGroup(GroupDaoImpl.java:32)

谢谢:)

最佳答案

Group 是一个 SQL 关键字。将您的表重命名为其他名称

@Table(name = "my_group")

关于java - 无法使用 Hibernate 创建实体。 SQL语法异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39689229/

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