Hibernate Text Table给我NumberFormatException

时间:2016-03-23 10:58:57

标签: hibernate hsqldb

我正在尝试使用Hibernate导入CSV文件,但它已经在第一行中给了我一个NumberFormatException。这是我的剧本:

CREATE SCHEMA PUBLIC AUTHORIZATION DBA
CREATE TEXT TABLE TKONTAKTE(KNUMMER INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY,KVORNAME VARCHAR(255) NOT NULL,KNACHNAME VARCHAR(255) NOT NULL,KSTRASSENUMMER VARCHAR(255) NOT NULL,KPOSTLEITZAHLORT VARCHAR(255) NOT NULL,KGEBURTSDATUM DATE,KNOTIZ LONGVARCHAR,KMAENNLICH CHAR(1) NOT NULL,KEMAIL VARCHAR(255) NOT NULL,CONSTRAINT SYS_CT_46 UNIQUE(KVORNAME,KNACHNAME))
SET TABLE TKONTAKTE SOURCE "Kontakte.csv;fs=\semi;ignore_first=true"
ALTER TABLE TKONTAKTE ALTER COLUMN KNUMMER RESTART WITH 1
CREATE USER SA PASSWORD ""
GRANT DBA TO SA
SET WRITE_DELAY 10

这是我的配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:hsqldb:file:testdb</property>
    <property name="connection.username">SA</property>
    <property name="connection.password"></property>
    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
    <property name="connection.shutdown">true</property>
      <property name="transaction.factory_class">
      org.hibernate.transaction.JDBCTransactionFactory
    </property>

    <property name="current_session_context_class">thread</property>
    <property name="transaction.auto_close_session">true</property>

    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <mapping resource="net/gobbz/kontakte/model/Kontakt.hbm.xml" />
  </session-factory>
</hibernate-configuration>

它会抛出以下错误:

11:53:57,813  WARN JDBCExceptionReporter:48 - SQL Warning: -98, SQLState: S1000
11:53:57,813  WARN JDBCExceptionReporter:49 -  bad TEXT table source file - line number: 1 java.lang.NumberFormatException: For input string: "Petra;Pichler;Kennedystr. 279;39055 Leifers;19.04.1988;;FALSCH;petra.pichler@gmail.com"
11:53:57,875 DEBUG SQL:346 - insert into tkontakte (kvorname, knachname, kstrasseNummer, kpostleitzahlOrt, kgeburtsdatum, knotiz, kmaennlich, kemail, knummer) values (?, ?, ?, ?, ?, ?, ?, ?, null)
Hibernate: insert into tkontakte (kvorname, knachname, kstrasseNummer, kpostleitzahlOrt, kgeburtsdatum, knotiz, kmaennlich, kemail, knummer) values (?, ?, ?, ?, ?, ?, ?, ?, null)
11:53:57,875  WARN JDBCExceptionReporter:71 - SQL Error: -32, SQLState: S1000
11:53:57,875 ERROR JDBCExceptionReporter:72 - The table data is read only in statement [insert into tkontakte (kvorname, knachname, kstrasseNummer, kpostleitzahlOrt, kgeburtsdatum, knotiz, kmaennlich, kemail, knummer) values (?, ?, ?, ?, ?, ?, ?, ?, null)]
11:53:57,875 ERROR TestKontakte:40 - org.hibernate.exception.GenericJDBCException could not insert: [net.gobbz.kontakte.model.Kontakt]

这里是Kontakt.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="net.gobbz.kontakte.model">
  <class name="Kontakt" table="tkontakte">
    <id name="nummer" column="knummer">
      <generator class="native"/>
    </id>
    <properties name="vornameNachname" unique="true">
        <property name="vorname" column="kvorname"
            type="string" not-null="true"/>
        <property name="nachname" column="knachname" 
            type="string" not-null="true"/>
    </properties>
    <property name="strasseNummer" column="kstrasseNummer" 
        type="string" not-null="true"/>
    <property name="postleitzahlOrt" column="kpostleitzahlOrt" 
        type="string" not-null="true"/>
    <property name="geburtsdatum" column="kgeburtsdatum" type="date"/>
    <property name="notiz" column="knotiz" type="text"/>
    <property name="maennlich" column="kmaennlich" type="yes_no" not-null="true"/>
    <property name="email" column="kemail" type="string" not-null="true"/>
  </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

您的表格列与DSV文件第一行中的数据不匹配。

  • 日期列的值格式不正确(1988-04-19)
  • KMAENNLICH CHAR(1)列的文字是FALSCH,太长了

将日期列的类型更改为varchar(8)。使用正确的列顺序。仅当文本源正确且SET TABLE TKONTAKTE SOURCE语句没有抛出异常时,才能执行更新语句。它抱怨表只是因为表没有有效的文本源而被读取。