问题描述:
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即可。