Hibernate:- 无法执行 JDBC 批量更新

发布于 2025-01-08 17:53:05 字数 2666 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

蓬勃野心 2025-01-15 17:53:05

转到 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>

长途伴 2025-01-15 17:53:05

此错误是由您的默认字符串大小字符引起的
你有两种选择来解决这个问题
1.必须增大字符串字符大小
示例

<property name="companyname" column="company_name" length='25' />

您的公司名称大小必须小于 25
2.将类型更改为文本
示例

<property name="companyname" column="company_name" type='text' />

this error coused by your default string size character
you have two choice for solve this problem
1.you must up string character size
example

<property name="companyname" column="company_name" length='25' />

your companyname size must little than 25
2. change type to text
example

<property name="companyname" column="company_name" type='text' />
飘过的浮云 2025-01-15 17:53:05
    Thera are 2 possibilities :
    1. Primary key issue. You are inserting the same value 2 times.
    2. You are using    
    SessionFactory factory=new AnnotationConfiguration().configure().buildSessionFactory(); 
    for creating sessionfactory OBJECT. It used for mysql.
So use following for oracle DB:
SessionFactory factory = new Configuration().configure().buildSessionFactory();
    for getting session.
    Thera are 2 possibilities :
    1. Primary key issue. You are inserting the same value 2 times.
    2. You are using    
    SessionFactory factory=new AnnotationConfiguration().configure().buildSessionFactory(); 
    for creating sessionfactory OBJECT. It used for mysql.
So use following for oracle DB:
SessionFactory factory = new Configuration().configure().buildSessionFactory();
    for getting session.
夜访吸血鬼 2025-01-15 17:53:05

该异常的原因之一可能是由于无效映射,我就遇到过这样的情况。当我浏览“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.

瞳孔里扚悲伤 2025-01-15 17:53:05

我的问题是表是在 SQL 中创建的,但显示空值
我通过更改数据库名称和创建新表来克服

My problem is table is created in SQL but showing null values
I overcome by changing the database name and creating new tables

深海里的那抹蓝 2025-01-15 17:53:05

我也有同样的问题,发现问题出在类名上。我命名了我的班级顺序,我发现顺序是一个关键字

[javax.persistence.criteria.Order]

我更新了我的类名,每件事都有效地工作

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

[javax.persistence.criteria.Order]

i update my class name and every thing work efficiently

一刻暧昧 2025-01-15 17:53:05

检查表中的列名。它们与您添加的内容匹配吗?我在 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.

倾其所爱 2025-01-15 17:53:05

此错误主要是由于未插入数据而发生的。
此问题可能有以下原因:

  1. 由于重复值。
  2. 您缺少任何必填字段/列。
  3. 由于错误的约束。

This error mainly occurs because data is not getting inserted.
There might be following reasons for this issue:

  1. Due to duplicate values.
  2. You are missing any mandatory field/column.
  3. Due to wrong constraint.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文