这个是精通hibernate里第16章的一个事例,运行出现如下错误
Hibernate: insert into CUSTOMERS (NAME, AGE, ID) values (?, ?, ?)
Hibernate: insert into IMAGES (CUSTOMER_ID, FILENAME) values (?, ?)
14:43:33,609  WARN JDBCExceptionReporter:57 - SQL Error: 1722, SQLState: 42000
14:43:33,609 ERROR JDBCExceptionReporter:58 - ORA-01722: 无效数字14:43:33,609 ERROR SessionImpl:2399 - Could not synchronize database state with session
Exception in thread "main" net.sf.hibernate.exception.SQLGrammarException: could not insert collection: [chapter16_1.mypack.Customer.image#3]
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
at net.sf.hibernate.collection.AbstractCollectionPersister.convert(AbstractCollectionPersister.java:728)
at net.sf.hibernate.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:545)
at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:23)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2395)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at chapter16_1.mypack.BusinessService.saveCustomer(BusinessService.java:28)
at chapter16_1.mypack.BusinessService.test(BusinessService.java:70)
at chapter16_1.mypack.BusinessService.main(BusinessService.java:86)
Caused by: java.sql.SQLException: ORA-01722: 无效数字具体的代码如下:
共有两个类BusinessService和Customer
  public class BusinessService{
  public static SessionFactory sessionFactory;
  static{
     try{
      // Create a configuration based on the properties file we've put
       Configuration config = new Configuration();
       config.addClass(Customer.class);
      // Get the session factory we can use for persistence
      sessionFactory = config.buildSessionFactory();
    }catch(Exception e){e.printStackTrace();}  }  public void saveCustomer(Customer customer) throws Exception{
      Session session = sessionFactory.openSession();
      Transaction tx = null;
          try {
       tx = session.beginTransaction();
       session.save(customer);
       tx.commit();
    }catch (Exception e) {
      if (tx != null) {
        // Something went wrong; discard all partial changes
        tx.rollback();
      }
      throw e;
    } finally {
      // No matter what, close the session
      session.close();
    }
  }   public void test() throws Exception{
      Set images=new HashSet();
      images.add("image1.jpg");
      images.add("image4.jpg");
      images.add("image2.jpg");
      images.add("image5.jpg");
      
      Customer customer=new Customer("Tom",21,images);
      saveCustomer(customer);
  }
  public static void main(String args[]) throws Exception {
    new BusinessService().test();
    sessionFactory.close();
  }
}customer类
public class Customer implements Serializable {
    private int id;
    private String name;
    private int age;
    private Set image=new TreeSet();    /** full constructor */
    public Customer(String name, int age,Set images) {
        this.name = name;
        this.age=age;
        this.image = images;
    }    /** default constructor */
    public Customer() {
    }    /** minimal constructor */
    public Customer(Set images) {
        this.image = images;
    }    public int getId() {
        return this.id;
    }    public void setId(int id) {
        this.id = id;
    }    public String getName() {
        return this.name;
    }    public void setName(String name) {
        this.name = name;
    }    public int getAge() {
        return this.age;
    }    public void setAge(int age) {
        this.age = age;
    }    public Set getImage() {
        return this.image;
    }    public void setImage(Set images) {
        this.image = images;
    }}
配置文件
<hibernate-mapping >  <class name="chapter16_1.mypack.Customer" table="CUSTOMERS" >
    <id name="id" type="int" column="ID">
      <generator class="increment"/>
    </id>    <property name="name" type="string" >
        <column name="NAME" length="15" />
    </property>   <property name="age" type="int" >
        <column name="AGE" />
   </property>   <set name="image"   table="IMAGES"  lazy="true" >
        <key column="CUSTOMER_ID" />
        <element column="FILENAME" type="string"  not-null="true"/>
   </set>   
  </class>
 
</hibernate-mapping>
数据库脚本
create table CUSTOMERS (
   ID int not null,
   NAME varchar(15),
   AGE int, 
   primary key (ID)
);create table IMAGES(
   CUSTOMER_ID int not null,
   FILENAME int not null,
   primary key (CUSTOMER_ID,FILENAME)
);alter table IMAGES  add constraint FK_CUSTOMER foreign key (CUSTOMER_ID) references CUSTOMERS(ID);
麻烦大家帮忙看一下,谢谢,是什么问题

解决方案 »

  1.   

    Hibernate: insert into CUSTOMERS (NAME, AGE, ID) values (?, ?, ?)
    Hibernate: insert into IMAGES (CUSTOMER_ID, FILENAME) values (?, ?)
    14:43:33,609 WARN JDBCExceptionReporter:57 - SQL Error: 1722, SQLState: 42000
    14:43:33,609 ERROR JDBCExceptionReporter:58 - ORA-01722: 无效数字检查插入记录时的数据值是多少,这里报错是说,你插入的数据不是一个数字,而这个字段是数字字段
      

  2.   

    或者可以说你输入的字符超出了你定义的字符长度? 如数字字符 int  跟long 是不一样的