- - PR -
HIBERNATEでDATEを扱うとエラーが
1
投稿者 | 投稿内容 | ||||
---|---|---|---|---|---|
|
投稿日時: 2005-02-10 11:57
こんにちは。HIBERNATE2.1.7C、j2sdk1.4.2_06です。Middlegenとhbm2javaを使用してマッピングファイルとEntityを自動生成するようにしています。
まだ勉強仕立てなのでテストの意味でSELECTを試してみようと思い、以下のようなコードを実行。 [コードの一部] List list = sess.find("FROM Zokugara"); for(Iterator ite = list.iterator(); ite.hasNext(){ Zokugara zokugara = (Zokugara)ite.next(); System.out.println(zokugara.getZokugaraid() + ":" + zokugara.getZokuname()); } DATE型のカラムがないテーブルのSELECTは問題なく表示できます。ただ、テーブルでDATEを含むテーブルのときのみ以下のエラーが出力されます。 [上部一部省略] 情報: building session factory 2005/02/10 11:51:08 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance 情報: Not binding factory to JNDI, no JNDI name configured 2005/02/10 11:51:08 net.sf.hibernate.util.JDBCExceptionReporter logExceptions 警告: SQL Error: 0, SQLState: S1009 2005/02/10 11:51:08 net.sf.hibernate.util.JDBCExceptionReporter logExceptions 致命的: Value '0000-00-00' can not be represented as java.sql.Date 2005/02/10 11:51:08 net.sf.hibernate.util.JDBCExceptionReporter logExceptions 警告: SQL Error: 0, SQLState: S1009 2005/02/10 11:51:08 net.sf.hibernate.util.JDBCExceptionReporter logExceptions 致命的: Value '0000-00-00' can not be represented as java.sql.Date net.sf.hibernate.exception.GenericJDBCException: Unable to perform find at net.sf.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:90) at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:79) at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30) at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4110) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3651) at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:238) at test.HibernateTest1.main(HibernateTest1.java:22) Caused by: java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date at com.mysql.jdbc.MysqlIO.unpackNativeEncodedColumn(MysqlIO.java:3919) at com.mysql.jdbc.MysqlIO.unpackBinaryResultSetRow(MysqlIO.java:3622) at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1285) at com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:2213) at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:414) at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:1902) at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:1350) at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1390) at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:957) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1657) at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:88) at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875) at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269) at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133) at net.sf.hibernate.loader.Loader.doList(Loader.java:1033) at net.sf.hibernate.loader.Loader.list(Loader.java:1024) at net.sf.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:118) at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3648) ... 2 more 見てみると、Caused by: java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date と出てる部分でDATE型が原因だという感じだと思います。WEBで調べてもなかなか解決策を見つけることができませんでした。同じような問題に遭遇された方、もしくはいい方法をご存知の方がいらっしゃいましたらよろしくご教示願います。 | ||||
|
投稿日時: 2005-02-10 13:52
その Zokugara テーブルに'0000-00-00'となっているデータがあって、java.util.Date オブジェクトへの変換を失敗している感じですね。
自動作成したマッピングファイルも載せてくれるともう少し分かるかもしれません。 | ||||
|
投稿日時: 2005-02-10 14:17
というありがたいお言葉をいただきましたので載せます。 エラーが出たほうのクラスに関係するファイルを載せます。 ***************** hibernate.cfg.xml *********************** <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <!-- Generated file - Do not edit! --> <hibernate-configuration> <!-- a SessionFactory instance listed as /jndi/name --> <session-factory> <!-- properties --> <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <property name="show_sql">false</property> <property name="use_outer_join">true</property> <property name="connection.username">****user</property> <property name="connection.password">****pass</property> <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property> <property name="connection.url">jdbc:mysql:///kamimoto</property> <!-- mapping files --> <mapping resource="entity/hibernate/Busyo.hbm.xml"/> <mapping resource="entity/hibernate/Yesno.hbm.xml"/> <mapping resource="entity/hibernate/Jspfileallow.hbm.xml"/> <mapping resource="entity/hibernate/Root.hbm.xml"/> <mapping resource="entity/hibernate/Jspjoballow.hbm.xml"/> <mapping resource="entity/hibernate/Syainmasta.hbm.xml"/> <mapping resource="entity/hibernate/Bloodtype.hbm.xml"/> <mapping resource="entity/hibernate/Gyousyasyurui.hbm.xml"/> <mapping resource="entity/hibernate/Eigyoujikantai.hbm.xml"/> <mapping resource="entity/hibernate/Eigyousakitaiou.hbm.xml"/> <mapping resource="entity/hibernate/Torihikisaki.hbm.xml"/> <mapping resource="entity/hibernate/Jspfileinfo.hbm.xml"/> <mapping resource="entity/hibernate/Menkyomasta.hbm.xml"/> <mapping resource="entity/hibernate/Jspjobinfo.hbm.xml"/> <mapping resource="entity/hibernate/Kazokukousei.hbm.xml"/> <mapping resource="entity/hibernate/Eigyousaki.hbm.xml"/> <mapping resource="entity/hibernate/Zokugara.hbm.xml"/> <mapping resource="entity/hibernate/Menkyojoukyou.hbm.xml"/> <mapping resource="entity/hibernate/Gyousyainfo.hbm.xml"/> <mapping resource="entity/hibernate/Eigyoujoukyou.hbm.xml"/> <mapping resource="entity/hibernate/Yakusyoku.hbm.xml"/> </session-factory> </hibernate-configuration> ************** Syainmasta.hbm.xml *************************** <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping > <class name="entity.hibernate.Syainmasta" table="syainmasta" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version" > <id name="syainid" column="syainid" type="java.lang.Integer" > <generator class="assigned"> <!-- To add non XDoclet generator parameters, create a file named hibernate-generator-params-Syainmasta.xml containing the additional parameters and place it in your merge dir. --> </generator> </id> <property name="busyoid" type="java.lang.Integer" update="true" insert="true" access="property" column="busyoid" length="11" /> <property name="yakusyokuid" type="java.lang.Integer" update="true" insert="true" access="property" column="yakusyokuid" length="11" /> <property name="simei" type="java.lang.String" update="true" insert="true" access="property" column="simei" length="100" /> <property name="simeiFuri" type="java.lang.String" update="true" insert="true" access="property" column="simei_furi" length="100" /> <property name="syasinPass" type="java.lang.String" update="true" insert="true" access="property" column="syasin_pass" length="100" /> <property name="tel" type="java.lang.String" update="true" insert="true" access="property" column="tel" length="50" /> <property name="juusyo" type="java.lang.String" update="true" insert="true" access="property" column="juusyo" length="150" /> <property name="birthday" type="java.util.Date" update="true" insert="true" access="property" column="birthday" length="10" /> <property name="bloodtypeid" type="java.lang.Integer" update="true" insert="true" access="property" column="bloodtypeid" length="11" /> <property name="keitaiTel" type="java.lang.String" update="true" insert="true" access="property" column="keitai_tel" length="30" /> <property name="keitaiMail" type="java.lang.String" update="true" insert="true" access="property" column="keitai_mail" length="100" /> <property name="mail" type="java.lang.String" update="true" insert="true" access="property" column="mail" length="100" /> <property name="syakaihokenTetuzukibi" type="java.util.Date" update="true" insert="true" access="property" column="syakaihoken_tetuzukibi" length="10" /> <!-- To add non XDoclet property mappings, create a file named hibernate-properties-Syainmasta.xml containing the additional properties and place it in your merge dir. --> </class> </hibernate-mapping> //SELECT実行用クラス エラーが出るのはDATE型を含むSyainmastaテーブルなので こちらを載せます ****************** HibernateTest1 [一部省略] ************************ Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); Session sess = sf.openSession(); Criteria cri = sess.createCriteria(Syainmasta.class); List list = cri.list(); for(Iterator ite = list.iterator(); ite.hasNext(){ Syainmasta syainmasta = (Syainmasta)ite.next(); System.out.println(syainmasta.getJuusyo() + ":" + syainmasta.getKeitaiMail()); } sess.close(); } catch(HibernateException e){ e.printStackTrace(); } | ||||
|
投稿日時: 2005-02-10 15:15
mysqlをお使いなんですね。新しいmysqlでは、0000-00-00を見るとExceptionを出すように変ったそうです。ドライバに指定するURLでzeroDateTimeBehaviorをconvertToNullに直す必要があります。
hibernate.cfg.xmlの中の <property name="connection.url">jdbc:mysql:///kamimoto</property> を、 <property name="connection.url">jdbc:mysql:///kamimoto?zeroDateTimeBehavior=convertToNull</property> にしてみてください mysqlは詳しくないので、違ったらごめんなさい。 | ||||
|
投稿日時: 2005-02-10 17:29
H2さんのご指摘のとおりに変更してみた結果、無事に表示することができました。
助かりました、深く感謝いたします。ありがとうございました。 |
1