“hibernate: 无法执行JDBC批量更新”

huangapple go评论134阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE hibernate-configuration PUBLIC
	&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;
	&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;
&lt;hibernate-configuration&gt;
&lt;session-factory&gt;
&lt;property name=&quot;hibernate.hbm2ddl.auto&quot;&gt;update&lt;/property&gt;
&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;
&lt;property name=&quot;dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
&lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql://localhost/june13?serverTimezone=UTC&lt;/property&gt;
&lt;property name=&quot;connection.username&quot;&gt;root&lt;/property&gt;
&lt;property name=&quot;connection.password&quot;&gt;root&lt;/property&gt;
&lt;property name=&quot;connection.driver_class&quot;&gt;com.mysql.cj.jdbc.Driver&lt;/property&gt;
&lt;mapping resource=&quot;Emp-hbm.xml&quot;/&gt;
&lt;/session-factory&gt;
&lt;/hibernate-configuration&gt;

Hibernate one-to-one mapping file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE hibernate-mapping PUBLIC 
    &quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;
    &quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;
&lt;hibernate-mapping&gt;
	&lt;class name=&quot;com.hibernate.one2one.Emp&quot; table=&quot;emp_906&quot;&gt;
		&lt;id name=&quot;id&quot; column=&quot;emp_id&quot;&gt;&lt;/id&gt;
		&lt;property name=&quot;name&quot; column=&quot;emp_name&quot; /&gt;
		&lt;one-to-one name=&quot;phoneno&quot; cascade=&quot;all&quot;&gt;&lt;/one-to-one&gt;
	&lt;/class&gt;
	&lt;class name=&quot;com.hibernate.one2one.PhoneNumber&quot; table=&quot;phone_906&quot;&gt;
		&lt;id name=&quot;pid&quot;&gt;&lt;/id&gt;
		&lt;property name=&quot;network&quot; /&gt;
		&lt;property name=&quot;phonenumber&quot; /&gt;
		&lt;one-to-one name=&quot;emp&quot;&gt;&lt;/one-to-one&gt;
	&lt;/class&gt;
&lt;/hibernate-mapping&gt;

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(&quot;Hibernate-cfg.xml&quot;);
		System.out.println(&quot;Configuration Object Created Successfully&quot;);
		SessionFactory sf=cfg.buildSessionFactory();
		System.out.println(&quot;Session Factory Object is Created&quot;);
		Session s=sf.openSession();
		Transaction t=s.beginTransaction();
		Emp e=new Emp();
		e.setId(104);
		e.setName(&quot;Lee Meng&quot;);
		PhoneNumber ph = new PhoneNumber();
		ph.setPid(201);
		ph.setNetwork(&quot;ATT&quot;);
		ph.setPhonenumber(&quot;9900336611&quot;);
		e.setPhoneno(ph);
		ph.setEmp(e);
		s.persist(e);
		t.commit();
		s.close();
		sf.close();
	}
}

The error code:

Exception in thread &quot;main&quot; 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 &#39;june13.emp_906&#39; doesn&#39;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 &#39;june13.emp_906&#39; doesn&#39;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 &lt;property name=&quot;hbm2ddl.auto&quot; value=&quot;create&quot;/&gt; 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.

huangapple
  • 本文由 发表于 2020年6月29日 04:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/62627679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定