Hibernate:- 无法执行 JDBC 批量更新
我是 Hibernate 新手,在尝试将 Friend_Job 对象保存在数据库上时遇到异常。
我正在从数据库获取 Friend 和 Job 对象并创建新的 Friend_Job 对象。
Test.java
SessionFactory sessionFectory = new Configuration().configure().buildSessionFactory();
Session session = sessionFectory.openSession();
Transaction transaction = session.beginTransaction();
Friend friend= (Friend) session.load(Friend.class, new Integer(1));
Job job = (Job) session.load(Job.class, new Integer(3));
Friend_Job friend_Job = new Friend_Job();
friend_Job.setFriend(friend);
friend_Job.setJob(job);
friend_Job.setCompanyName(job.getCompanyName());
session.save(friend_Job);
transaction.commit(); //Exception here
Friend_Job.hbm.xml
<hibernate-mapping>
<class name="hibernateTest.Friend_Job" table="FRIEND_JOB">
<id name="primaryKey" column="PRIMARY_KEY">
<generator class="increment"/>
</id>
<property name="companyName" type="string" column="COMPANY_NAME"/>
<property name="salary" column="SALARY"/>
<many-to-one name="friend" class="hibernateTest.Friend" cascade="none" column="FK_FRIEND_ID"/>
<many-to-one name="job" class="hibernateTest.Job" cascade="none" column="FK_JOB_ID"/>
</class>
</hibernate-mapping>
异常:-
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at hibernateTest.Test.main(Test.java:20)
引起:java.sql.BatchUpdateException:ORA-00932:数据类型不一致:预期 BINARY 得到 NUMBER
Friend_Job.java
public class Friend_Job {
private int primaryKey;
private String companyName;
private int salary = 0;
private Friend friend;
private Job job;
,其他是 setter 和 getter。
如果需要更多信息,请告诉我...... 提前致谢。
i am new in Hibernate, i am getting exception when trying to save Friend_Job object on database.
I am getting Friend and Job object from database and creating new Frien_Job object.
Test.java
SessionFactory sessionFectory = new Configuration().configure().buildSessionFactory();
Session session = sessionFectory.openSession();
Transaction transaction = session.beginTransaction();
Friend friend= (Friend) session.load(Friend.class, new Integer(1));
Job job = (Job) session.load(Job.class, new Integer(3));
Friend_Job friend_Job = new Friend_Job();
friend_Job.setFriend(friend);
friend_Job.setJob(job);
friend_Job.setCompanyName(job.getCompanyName());
session.save(friend_Job);
transaction.commit(); //Exception here
Friend_Job.hbm.xml
<hibernate-mapping>
<class name="hibernateTest.Friend_Job" table="FRIEND_JOB">
<id name="primaryKey" column="PRIMARY_KEY">
<generator class="increment"/>
</id>
<property name="companyName" type="string" column="COMPANY_NAME"/>
<property name="salary" column="SALARY"/>
<many-to-one name="friend" class="hibernateTest.Friend" cascade="none" column="FK_FRIEND_ID"/>
<many-to-one name="job" class="hibernateTest.Job" cascade="none" column="FK_JOB_ID"/>
</class>
</hibernate-mapping>
Exception :-
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at hibernateTest.Test.main(Test.java:20)
Caused by: java.sql.BatchUpdateException: ORA-00932: inconsistent datatypes: expected BINARY got NUMBER
Friend_Job.java
public class Friend_Job {
private int primaryKey;
private String companyName;
private int salary = 0;
private Friend friend;
private Job job;
and else are setter and getter.
Please let me know if more info required....
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
转到
hibernate.cfg.xml
检查
<属性名称=“hbm2ddl.auto”>创建
如果有create则改为“update”
<属性名称=“hbm2ddl.auto”>更新
Go to
hibernate.cfg.xml
Check
<property name="hbm2ddl.auto">create</property>
If there is create then change to "update"
<property name="hbm2ddl.auto">update</property>
该异常的原因之一可能是由于无效映射,我就遇到过这样的情况。当我浏览“hbm”文件时,有一个到子表的 Set 映射,我在其中指定了子表中不存在的列名(该列不存在于表中)。在我在映射中指定正确的列后,问题立即得到解决。
可能还有其他原因,但目前我只知道这些,希望对您有所帮助。
One of the reason for that exception can be due to invalid mapping, I had such a case. When I went through the 'hbm' file there was a Set mapping to a child table, where I specified the column name which is not in the child table (the column was not present in the table). The issue was solved right after I specified the proper column in mapping.
There might be other reasons, but for now this is all I know, hope it helps.
我的问题是表是在 SQL 中创建的,但显示空值
我通过更改数据库名称和创建新表来克服
My problem is table is created in SQL but showing null values
I overcome by changing the database name and creating new tables
我也有同样的问题,发现问题出在类名上。我命名了我的班级顺序,我发现顺序是一个关键字
我更新了我的类名,每件事都有效地工作
I have the same problem and find that the problem came from the class name . i named my class order and i found that Order is a keyword
i update my class name and every thing work efficiently
检查表中的列名。它们与您添加的内容匹配吗?我在 hbm.xml 和实际表中输入不同的列名称,它是不同的。
Check for column name in table. Are they matching with what you have added. I was entering different column name in hbm.xml and in actual table,it was different.
此错误主要是由于未插入数据而发生的。
此问题可能有以下原因:
This error mainly occurs because data is not getting inserted.
There might be following reasons for this issue: