【问题标题】:Hibernate :- Could not execute JDBC batch updateHibernate :- 无法执行 JDBC 批量更新
【发布时间】:2012-02-23 17:15:48
【问题描述】:

我是 Hibernate 的新手,尝试将 Friend_Job 对象保存到数据库时遇到异常。

我正在从数据库中获取 Friend 和 Job 对象并创建新的 Frien_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;

and else 是 setter 和 getter。

如果需要更多信息,请告诉我.... 提前致谢。

【问题讨论】:

  • FRIEND_JOB 表中的哪一列是二进制类型?
  • @Firo :- 没有任何列类型二进制,我不知道为什么会抛出这个异常。

标签: hibernate


【解决方案1】:

转到hibernate.cfg.xml

检查
&lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;
如果有创建则更改为“更新”
&lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:
          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.
      

      【讨论】:

        【解决方案4】:

        该异常的一个原因可能是由于映射无效,我有这样的情况。当我浏览“hbm”文件时,有一个到子表的 Set 映射,我在其中指定了不在子表中的列名(该列不存在于表中)。在我在映射中指定了正确的列后,问题就解决了。 可能还有其他原因,但目前我只知道这些,希望对您有所帮助。

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            我有同样的问题,发现问题来自类名。我命名了我的班级订单,我发现订单是一个关键字

            [javax.persistence.criteria.Order]

            我更新了我的班级名称,一切都高效地工作

            【讨论】:

            • 请不要在多个旧问题中继续发布相同的答案。如果您认为它们是重复的,请标记为重复。 link
            【解决方案7】:

            检查表中的列名。它们是否与您添加的内容匹配。我在 hbm.xml 和实际表中输入了不同的列名,它是不同的。

            【讨论】:

              【解决方案8】:

              这个错误主要是因为没有插入数据。 此问题可能有以下原因:

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

              【讨论】:

                猜你喜欢
                • 2014-02-20
                • 2020-10-18
                • 2012-07-30
                • 1970-01-01
                • 2012-03-13
                • 2011-04-05
                • 1970-01-01
                相关资源
                最近更新 更多