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

时间:2012-02-23 17:15:48

标签: hibernate

我是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;

而且是setter和getter。

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

7 个答案:

答案 0 :(得分:1)

转到hibernate.cfg.xml

检查
<property name="hbm2ddl.auto">create</property>
如果有创建,则更改为&#34;更新&#34;
<property name="hbm2ddl.auto">update</property>

答案 1 :(得分:0)

  

此错误由您的默认字符串大小字符
组成   你有两个选择来解决这个问题   1.你必须要字符串大小
  示例

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

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

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

答案 2 :(得分:0)

    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.

答案 3 :(得分:0)

该异常的原因之一可能是由于映射无效,我遇到过这样的情况。当我浏览'hbm'文件时,有一个Set映射到子表,在那里我指定了不在子表中的列名(表中没有该列)。我在映射中指定了正确的列后立即解决了该问题。        可能还有其他原因,但现在这就是我所知道的,希望它有所帮助。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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

  

[javax.persistence.criteria.Order]

我更新了我的班级名称,一切都有效

答案 6 :(得分:-1)

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.