英文:
hibernate: Could not execute JDBC batch update
问题
以下是您提供的代码部分的翻译:
package com.hibernate.one2one;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class App
{
public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure("Hibernate-cfg.xml");
System.out.println("成功创建配置对象");
SessionFactory sf = cfg.buildSessionFactory();
System.out.println("成功创建会话工厂对象");
Session s = sf.openSession();
Transaction t = s.beginTransaction();
Emp e = new Emp();
e.setId(104);
e.setName("Lee Meng");
PhoneNumber ph = new PhoneNumber();
ph.setPid(201);
ph.setNetwork("ATT");
ph.setPhonenumber("9900336611");
e.setPhoneno(ph);
ph.setEmp(e);
s.persist(e);
t.commit();
s.close();
sf.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost/june13?serverTimezone=UTC</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<mapping resource="Emp-hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.one2one.Emp" table="emp_906">
<id name="id" column="emp_id"></id>
<property name="name" column="emp_name" />
<one-to-one name="phoneno" cascade="all"></one-to-one>
</class>
<class name="com.hibernate.one2one.PhoneNumber" table="phone_906">
<id name="pid"></id>
<property name="network" />
<property name="phonenumber" />
<one-to-one name="emp"></one-to-one>
</class>
</hibernate-mapping>
希望这对您有所帮助。如果您有任何其他疑问,请随时问我。
英文:
When I want to create a one to one mapping by using hibernate, the error "Could not execute JDBC batch update" keeps raising and I noticed that it was caused by the failure of creating tables in the MySQL according to the error it showed. Would you please help me to find out the problems inside it? Thank you!
Hibernate Configuration File
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost/june13?serverTimezone=UTC</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<mapping resource="Emp-hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate one-to-one mapping file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.one2one.Emp" table="emp_906">
<id name="id" column="emp_id"></id>
<property name="name" column="emp_name" />
<one-to-one name="phoneno" cascade="all"></one-to-one>
</class>
<class name="com.hibernate.one2one.PhoneNumber" table="phone_906">
<id name="pid"></id>
<property name="network" />
<property name="phonenumber" />
<one-to-one name="emp"></one-to-one>
</class>
</hibernate-mapping>
Main File:
package com.hibernate.one2one;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class App
{
public static void main(String[] args)
{
Configuration cfg=new Configuration();
cfg.configure("Hibernate-cfg.xml");
System.out.println("Configuration Object Created Successfully");
SessionFactory sf=cfg.buildSessionFactory();
System.out.println("Session Factory Object is Created");
Session s=sf.openSession();
Transaction t=s.beginTransaction();
Emp e=new Emp();
e.setId(104);
e.setName("Lee Meng");
PhoneNumber ph = new PhoneNumber();
ph.setPid(201);
ph.setNetwork("ATT");
ph.setPhonenumber("9900336611");
e.setPhoneno(ph);
ph.setEmp(e);
s.persist(e);
t.commit();
s.close();
sf.close();
}
}
The error code:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2411)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2874)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.hibernate.one2one.App.main(App.java:32)
Caused by: java.sql.BatchUpdateException: Table 'june13.emp_906' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.util.Util.handleNewInstance(Util.java:192)
at com.mysql.cj.util.Util.getInstance(Util.java:167)
at com.mysql.cj.util.Util.getInstance(Util.java:174)
at com.mysql.cj.jdbc.exceptions.SQLError.createBatchUpdateException(SQLError.java:224)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchSerially(ClientPreparedStatement.java:853)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchInternal(ClientPreparedStatement.java:435)
at com.mysql.cj.jdbc.StatementImpl.executeBatch(StatementImpl.java:796)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 15 more
Caused by: java.sql.SQLSyntaxErrorException: Table 'june13.emp_906' doesn't exist
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchSerially(ClientPreparedStatement.java:832)
... 19 more
答案1
得分: 1
关于在你的 Hibernate 配置文件中添加 <property name="hbm2ddl.auto" value="create"/>
,这将确保在启动时创建表并保持其完整性。
validate: 验证模式,对架构进行验证,不对数据库进行更改。
update: 更新模式,更新架构。
create: 创建模式,创建架构,销毁之前的数据。
create-drop: 在显式关闭 SessionFactory 时丢弃架构,通常在应用程序停止时发生。
none: 无操作模式,不对架构进行任何更改。
关于可能的值的更详细说明:
https://stackoverflow.com/questions/438146/what-are-the-possible-values-of-the-hibernate-hbm2ddl-auto-configuration-and-wha
英文:
How about adding <property name="hbm2ddl.auto" value="create"/>
to your hibernate configruation file. This will ensure your tables are created on startup and leave them intact.
> validate: validate the schema, makes no changes to the database.
> update: update the schema. create: creates the schema, destroying
> previous data. create-drop: drop the schema when the SessionFactory is
> closed explicitly, typically when the application is stopped. none:
> does nothing with the schema, makes no changes to the database
A more extensive description of the possible values :
https://stackoverflow.com/questions/438146/what-are-the-possible-values-of-the-hibernate-hbm2ddl-auto-configuration-and-wha
答案2
得分: 0
这是一个Java错误:请在MySQL的my.cnf文件中禁用/注释掉恢复部分。
因为现在MySQL处于一种状态,用户无法在MySQL数据库上进行任何更改/编辑。
英文:
It is a java error: Please disable/comment out recovery part from MySQL my.cnf file.
As now the MySQL is in a state where users cannot change/edit anything on the MySQL database.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论