问题描述:

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

 package com.tt.hibernate.helloworld;

 import java.sql.Date;

 public class News {

     private Integer id;
private String title;
private String author; private Date date; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
} public News(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
} public News(){ } @Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
} }

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

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory> <!-- 配置数据库的基本信息 -->
<property name="conncection.username">root</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Hibernate</property> <!-- 配置hibernate的基本信息 -->
<!-- hibernate所使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 执行操作时是否在控制台打印SQL -->
<property name="hibernate.show_sql">true</property> <!-- 是否对SQL进行格式化 -->
<property name="hibernate.format_sql">true</property> <!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property> <!-- 指定关联的 .hbm.xml文档 -->
<mapping resource="com/tt/hibernate/helloworld/News.hbm.xml"/> </session-factory>
</hibernate-configuration>

News.hbm.xml(对象关系映射文件类)

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-11-28 11:43:38 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.tt.hibernate.helloworld.News" table="NEWS"> <id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主键的生成方式,native:使用数据库本地方式 -->
<generator class="native" />
</id> <property name="title" type="java.lang.String">
<column name="TITLE" />
</property> <property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property> <property name="date" type="java.util.Date">
<column name="DATE" />
</property> </class> </hibernate-mapping>

HibernateTest.java

 package com.tt.hibernate.helloworld;

 import java.sql.Date;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test; public class HibernateTest { @Test
public void test() {
//1.创建一个SessionFactory对象
SessionFactory sessionFactory = null; //1).创建configuration对象:对应hibernate的基本配置信息和对象关系映射信息
Configuration configuration = new Configuration().configure(); //4.0之前这样创建
//sessionFactory = configuration.buildSessionFactory(); //2).创建一个ServiceRegistry对象:hibernate4.x新添加的对象
//hibernate的任何配置和服务都需要在该对象中注册后才能有效
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry(); //3).
sessionFactory = configuration.buildSessionFactory(serviceRegistry); //2.创建一个Session对象
Session session = sessionFactory.openSession(); //3.开启事务
Transaction transaction = session.beginTransaction(); //4.执行保存操作
News news = new News("Java","tt",new Date(new java.util.Date().getTime()));
session.save(news); News news2 = (News) session.get(News.class, 1);
System.out.println(news2); //5.提交事务
transaction.commit(); //6.关闭Session
session.close(); //7.关闭SessionFactory对象
sessionFactory.close();
} }

可以看出在News.java里的变量date导入的是java.sql.date包,在对应的News.hbm.xml文件里定义的变量date是java.util.Date类型,而在HibernateTest.java里传入的是java.sql.Timestamp。

      

解决办法:

将News.java里的date变量的包和其他文件一样统一成java.util.Date即可。

ERROR: HHH000123: IllegalArgumentException in class: com.tt.hibernate.helloworld.News, setter method of property: date的更多相关文章

  1. 报错:org.hibernate.AssertionFailure: null id in com.tt.hibernate.entities.News entry (don't flush the Session after an exception occurs)

    在使用hibernate创建数据库的表格时,出现了如下报错: 十二月 28, 2016 10:17:02 上午 org.hibernate.tool.hbm2ddl.SchemaExport perf ...

  2. Spring整合Hibernate报错:annotatedClasses is not writable or has an invalid setter method

    Spring 整合Hibernate时报错: org.springframework.beans.factory.BeanCreationException: Error creating bean ...

  3. hibernate ——helloWorld程序(annotation配置)

    在  <hibernate  ——helloWorld程序(XML配置)>基础上,修改.添加部分文件: 1.Teacher类和Teacher表 package com.pt.hiberna ...

  4. Error during generated code invocation: com.intellij.debugger.engine.evaluation.EvaluateException: Method threw 'java.lang.IllegalAccessError' exception.

    场景描述: 再从该数据库中读取数据进行处理的时候,需要将某个字段加入到一个动态的map中,然后需要对该map进行filter过滤,在执行过滤方法的时候报错 Error during generated ...

  5. 报错:An error occurred at line: 22 in the generated java file The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

    org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 22 in ...

  6. Hibernate HelloWorld案例

    搭建一个Hibernate环境,开发步骤: 1. 下载源码 版本:hibernate-distribution-3.6.0.Final 2. 引入jar文件          hibernate3.j ...

  7. hibernate处理null 时提示:Property path [...] does notreference a collection

    Hibernate判断某属性不为null 且不可为空时出现Property path [...] does notreference a collection 的问题 处理空的方法: isNotEmp ...

  8. hibernate ——helloWorld程序(XML配置)

    1.项目结构 2.hibernate实现了Java类 和 数据库表的映射(Map) 先看一下Student的Java类和对应的数据库表 package com.pt.hibernate; public ...

  9. hibernate出现QueryException: could not resolve property 查询异常

    可能是你的属性名写错了,  因为hibernate是面向对象和属性的.

随机推荐

  1. 【Learning Python】【第二章】Python基础类型和基础操作

    基础类型: 整型: py 3.0解决了整数溢出的问题,意味着整型不必考虑32位,64位,有无符号等问题,你写一个1亿亿亿,就是1亿亿亿,不会溢出 a = 10 ** 240 print(a) 执行以上 ...

  2. gulp问题

    刚刚又碰到gulp的一个小问题,就是改变src下的index.scss时碰到问题后监听就会立即停止,这很蛋疼: 解决办法就是在gulpfile.js中做一点改变:

  3. LVM逻辑卷基本概念及LVM的工作原理

    这篇随笔将详细讲解Linux磁盘管理机制中的LVM逻辑卷的基本概念以及LVM的工作原理!!! 一.传统的磁盘管理 其实在Linux操作系统中,我们的磁盘管理机制和windows上的差不多,绝大多数都是 ...

  4. iOS--时间类date详解

    NSDate定义时间的类 NSDate是一个时间类,在编写程序时,我们很少遇到.今天我突然碰到,感觉很生疏. 给大家发个博客,让大家也都温习一下,哈哈! 兄弟用的时候突然发现竟然有一些bug,大家用时 ...

  5. VB 中Sub和Function的区别

    Sub可以理解为执行一个过车,一个操作. Function在执行完过后,还要返回一个结果. Sub:过程:Function:函数,可以带返回值. 语法: Sub SubName(参数1,参数2,... ...

  6. PLS-00306错误

    ORA-06550: line 1, column 7:PLS-00306: wrong number or types of arguments in call to 'P'ORA-06550: l ...

  7. 在博文中嵌入Javascript代码

    今天吃饭时无聊,突然想到Markdown除了兼容HTML会不会也兼容Javascript,于是博文里除了码文插音乐还可以干点更好玩的事儿了,可以自动修改markdown文件本身,比如说自动修改从Git ...

  8. Python学习-day1

    Mark一下,python学习. 今天一天已1.5x的速度看完了Alex老师的第一周的视频,先是4节鸡汤课,而且给了勺,讲述了python目前在世界的地位,发展趋势,以及未来的愿景. 最重要的还是写一 ...

  9. 网站统计中的数据收集原理及实现(share)

    转载自:http://blog.codinglabs.org/articles/how-web-analytics-data-collection-system-work.html 网站数据统计分析工 ...

  10. 关于URI URL URN

    刚琢磨.整理了关于escape.encodeURIComponent.encodeURI的知识.突然又对URI有点模糊了,遂整理了以下资源 : 资源一: URL,URI 和URN 的举例理解 资源二: ...