2012-02-23 52 views
3

我是Hibernate中的新手,在尝试将Friend_Job对象保存到数据库时出现异常。休眠: - 无法执行JDBC批量更新

我从数据库中获取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; 

否则是设置者和获取者。

请让我知道是否需要更多信息.... 在此先感谢。

+0

其列的表'FRIEND_JOB'是二进制类型的空值 我克服产生的? – Firo 2012-02-24 08:48:40

+0

@Firo: - 没有任何列类型的二进制文件,我不知道为什么这个异常抛出。 – 2012-02-29 04:42:42

回答

1

转到hibernate.cfg.xml

检查
<property name="hbm2ddl.auto">create</property>
如果创建然后更改为 “更新”
<property name="hbm2ddl.auto">update</property>

0

此错误您的默认字符串大小字符致毒
你有两个解决此问题的选择
1.you必须加上字符串字符大小
例如

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

您的公司名称的大小必须略高于25
2.变型为文本 例如

<property name="companyname" column="company_name" type='text' /> 
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. 
0

其中一个原因是例外可以由于无效的映射,我有例如。当我浏览'hbm'文件时,有一个Set映射到一个子表,其中我指定了不在子表中的列名(该列在表中不存在)。我在映射中指定了正确的列后,问题就解决了。 可能还有其他原因,但现在这是我所知道的,希望它有帮助。

-1

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

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

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

相关问题