ERROR: HHH000123: IllegalArgumentException in class: com.tt.hibernate.helloworld.News, setter method of property: date


問題描述:

Hibernate連接數據庫時,報出如下錯誤:

十一月 29, 2016 3:08:31 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate:
select
news0_.ID as ID1_0_0_,
news0_.TITLE as TITLE2_0_0_,
news0_.AUTHOR as AUTHOR3_0_0_,
news0_.DATE as DATE4_0_0_
from
NEWS news0_
where
news0_.ID=?
十一月 29, 2016 3:08:32 下午 org.hibernate.property.BasicPropertyAccessor$BasicSetter set
ERROR: HHH000123: IllegalArgumentException in class: com.tt.hibernate.helloworld.News, setter method of property: date
十一月 29, 2016 3:08:32 下午 org.hibernate.property.BasicPropertyAccessor$BasicSetter set
ERROR: HHH000091: Expected type: java.sql.Date, actual value: java.sql.Timestamp
十一月 29, 2016 3:08:32 下午 org.hibernate.event.internal.DefaultLoadEventListener onLoad
INFO: HHH000327: Error performing load command : org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.tt.hibernate.helloworld.News.date

原因分析:

查看報錯信息:說是News類的屬性date的非法聲明異常,查看出現date的地方:

News.java

 1 package com.tt.hibernate.helloworld;  2 
 3 import java.sql.Date;  4 
 5 public class News {  6 
 7     private Integer id;  8     private String title;  9     private String author; 10     
11     private Date date; 12 
13     public Integer getId() { 14         return id; 15  } 16 
17     public void setId(Integer id) { 18         this.id = id; 19  } 20 
21     public String getTitle() { 22         return title; 23  } 24 
25     public void setTitle(String title) { 26         this.title = title; 27  } 28 
29     public String getAuthor() { 30         return author; 31  } 32 
33     public void setAuthor(String author) { 34         this.author = author; 35  } 36 
37     public Date getDate() { 38         return date; 39  } 40 
41     public void setDate(Date date) { 42         this.date = date; 43  } 44 
45     public News(String title, String author, Date date) { 46         super(); 47         this.title = title; 48         this.author = author; 49         this.date = date; 50  } 51     
52     public News(){ 53         
54  } 55 
56  @Override 57     public String toString() { 58         return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]"; 59  } 60     
61     
62     
63 }

hibernate.cfg.xml(Hibernate配置文件)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC  3  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  4  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7            
 8         <!-- 配置數據庫的基本信息 -->
 9         <property name="conncection.username">root</property>
10         <property name="connection.password">1234</property>
11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12         <property name="connection.url">jdbc:mysql://localhost:3306/Hibernate</property>
13 
14 
15         <!-- 配置hibernate的基本信息 -->
16         <!-- hibernate所使用的數據庫方言 -->
17         <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
18 
19         <!-- 執行操作時是否在控制台打印SQL -->
20         <property name="hibernate.show_sql">true</property>
21 
22         <!-- 是否對SQL進行格式化 -->
23         <property name="hibernate.format_sql">true</property>
24 
25         <!-- 指定自動生成數據表的策略 -->
26         <property name="hbm2ddl.auto">update</property>
27         
28         <!-- 指定關聯的 .hbm.xml文檔 -->
29         <mapping resource="com/tt/hibernate/helloworld/News.hbm.xml"/>
30         
31     </session-factory>
32 </hibernate-configuration>

News.hbm.xml(對象關系映射文件類)

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2016-11-28 11:43:38 by Hibernate Tools 3.5.0.Final -->
 5 <hibernate-mapping>
 6     <class name="com.tt.hibernate.helloworld.News" table="NEWS">
 7     
 8         <id name="id" type="java.lang.Integer">
 9             <column name="ID" />
10             <!-- 指定主鍵的生成方式,native:使用數據庫本地方式 -->
11             <generator class="native" />
12         </id>
13         
14         <property name="title" type="java.lang.String">
15             <column name="TITLE" />
16         </property>
17         
18         <property name="author" type="java.lang.String">
19             <column name="AUTHOR" />
20         </property>
21         
22         <property name="date" type="java.util.Date">
23             <column name="DATE" />
24         </property>
25         
26     </class>
27     
28 </hibernate-mapping>

HibernateTest.java

 1 package com.tt.hibernate.helloworld;  2 
 3 import java.sql.Date;  4 
 5 import org.hibernate.Session;  6 import org.hibernate.SessionFactory;  7 import org.hibernate.Transaction;  8 import org.hibernate.cfg.Configuration;  9 import org.hibernate.service.ServiceRegistry; 10 import org.hibernate.service.ServiceRegistryBuilder; 11 import org.junit.Test; 12 
13 public class HibernateTest { 14 
15  @Test 16     public void test() { 17         //1.創建一個SessionFactory對象
18         SessionFactory sessionFactory = null; 19         
20         //1).創建configuration對象:對應hibernate的基本配置信息和對象關系映射信息
21         Configuration configuration = new Configuration().configure(); 22         
23         //4.0之前這樣創建 24         //sessionFactory = configuration.buildSessionFactory(); 25         
26         //2).創建一個ServiceRegistry對象:hibernate4.x新添加的對象 27         //hibernate的任何配置和服務都需要在該對象中注冊后才能有效
28         ServiceRegistry serviceRegistry =
29                 new ServiceRegistryBuilder().applySettings(configuration.getProperties()) 30  .buildServiceRegistry(); 31         
32         //3).
33         sessionFactory = configuration.buildSessionFactory(serviceRegistry); 34                 
35        //2.創建一個Session對象
36         Session session = sessionFactory.openSession(); 37         
38        //3.開啟事務
39         Transaction transaction = session.beginTransaction(); 40         
41        //4.執行保存操作 42         News news = new News("Java","tt",new Date(new java.util.Date().getTime())); 43         session.save(news);
44         
45         News news2 = (News) session.get(News.class, 1); 46  System.out.println(news2); 47         
48        //5.提交事務
49  transaction.commit(); 50         
51       //6.關閉Session
52  session.close(); 53         
54       //7.關閉SessionFactory對象
55  sessionFactory.close(); 56  } 57 
58 }

可以看出在News.java里的變量date導入的是java.sql.date包,在對應的News.hbm.xml文件里定義的變量date是java.util.Date類型,而在HibernateTest.java里傳入的是java.sql.Timestamp。

      

解決辦法:

將News.java里的date變量的包和其他文件一樣統一成java.util.Date即可。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM