问题描述:

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. js/jquery 回调函数的定义方法

    基本写法: 带参数的回调函数 以上回调函数,直接传入function作为参数,同样,还可以传入json对象作为参数...如下. 该方法的优势是可以定义多个回调函数....类似$.ajax回调函数中的s ...

  2. C++注意事项

    1.static和const不能同时修饰类的成员函数(static int getde()const;) 分析:原因在于const会在函数中添加一个隐式参数const this*,而static是没有 ...

  3. SqlServer性能优化 通过压缩与计算列提高性能(十一)

    压缩: 1.压缩的对象 1.表   2.索引(非聚集索引手工做)   3.备份(手工做) 2.对性能影响 1.提高IO性能     2.降低CPU性能 行压缩: 1.对null值不占用空间 2.对Nu ...

  4. 使用Cargo实现自动化部署

    Cargo是一组帮助用户操作Web容器的工具,它能帮助用户实现自动化部署,而且它几乎支持所有的Web容器,如Tomcat.JBoss.Jetty和Glassfish.Cargo通过cargo-mave ...

  5. 爆破一个二元函数加密的cm

    系统 : Windows xp 程序 : cztria~1 程序下载地址 :http://pan.baidu.com/s/1slUwmVr 要求 : 爆破 使用工具 : OD 可在看雪论坛中查找关于此 ...

  6. git学习:关于origin和master

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2016/10/27 本文主要是对这篇博客文章的理解. git的服务器端(remote)端包含多个repository, ...

  7. gdb 调试出现 ImportError: No module named 'libstdcxx'

    在emacs使用gdb调试程序,出现错误 , in <module> from libstdcxx.v6.printers import register_libstdcxx_printe ...

  8. CCI4.5/LintCode Validate Binary Search Tree

    Validate BST是指按中序遍历niorder后左<node<右: 第一种方法: 先按inoreder遍历, 再放进ArrayList里用循环看是不是从小到大排序: 注意: 设置成员 ...

  9. 创建Maven web项目时 出现 web.xml is missing and <failOnMissingWebXml> is set to true错误 pox.xml编译错误

    今天创建一个maven项目 pom.xml出现如下错误: web.xml is missing and <failOnMissingWebXml> is set to true 这是因为你 ...

  10. Pattern Recognition And Machine Learning (模式识别与机器学习) 笔记 (1)

    By Yunduan Cui 这是我自己的PRML学习笔记,目前持续更新中. 第二章 Probability Distributions 概率分布 本章介绍了书中要用到的概率分布模型,是之后章节的基础 ...