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
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的更多相关文章
- 报错: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 ...
- Spring整合Hibernate报错:annotatedClasses is not writable or has an invalid setter method
Spring 整合Hibernate时报错: org.springframework.beans.factory.BeanCreationException: Error creating bean ...
- hibernate ——helloWorld程序(annotation配置)
在 <hibernate ——helloWorld程序(XML配置)>基础上,修改.添加部分文件: 1.Teacher类和Teacher表 package com.pt.hiberna ...
- Error during generated code invocation: com.intellij.debugger.engine.evaluation.EvaluateException: Method threw 'java.lang.IllegalAccessError' exception.
场景描述: 再从该数据库中读取数据进行处理的时候,需要将某个字段加入到一个动态的map中,然后需要对该map进行filter过滤,在执行过滤方法的时候报错 Error during generated ...
- 报错: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 ...
- Hibernate HelloWorld案例
搭建一个Hibernate环境,开发步骤: 1. 下载源码 版本:hibernate-distribution-3.6.0.Final 2. 引入jar文件 hibernate3.j ...
- hibernate处理null 时提示:Property path [...] does notreference a collection
Hibernate判断某属性不为null 且不可为空时出现Property path [...] does notreference a collection 的问题 处理空的方法: isNotEmp ...
- hibernate ——helloWorld程序(XML配置)
1.项目结构 2.hibernate实现了Java类 和 数据库表的映射(Map) 先看一下Student的Java类和对应的数据库表 package com.pt.hibernate; public ...
- hibernate出现QueryException: could not resolve property 查询异常
可能是你的属性名写错了, 因为hibernate是面向对象和属性的.
随机推荐
- Myeclipse+AJAX+Servlet
最近刚开始学AJAX的知识,这里介绍一个简单的Myeclipse+AJAX+Servlet项目. 此项目包含3个文件:index.jsp.check.java.还有一个需要配置的文件是:web.xml ...
- Android Studio Problem : failed to find style 'textviewstyle' in current theme 解决方法
新建一个空白的MainActivity时Preview就出现一个错误: failed to find style 'textviewstyle' in current theme 开始在国内的博客平台 ...
- Android Studio安装插件GsonFormat
Android Studio菜单栏File > Settings > plugins' 这个是Android Studio搜索和安装插件的界面,下面直接上动图 : 安装结束后需要关闭重新启 ...
- [转] Jenkins实战演练之Windows系统节点管理
[前提] 通过<Jenkins实战演练之Windows服务器快速搭建>(http://my.oschina.net/iware/blog /191818)和<Jenkins实战演练之 ...
- javscript创建Emitter
本文简单叙述下javascript是如何建立一个Emitter构造函数的. /** * 定义Emitter构造函数 */ function Emitter() { } /** * 添加监听事件 */ ...
- javascript下ie7,ie8的Date Bug的解决
ie9+, chrome firefox opera下 string到Date 使用 new Date("2013-01-01"); 都是ok的. 但在ie7, ie8下 返回N ...
- archlinux vmware一些问题
虚拟机没法上网 sudo modprobe vmnet sudo vmware-network --start
- SSM框架的整合思路&功能实现
这是我第一篇博客,关于SSM框架的整合思路以及简单功能实现. 首先,最近刚刚学习Spring+SpringMVC+Mybatis,在开发时遇到形形色色的问题,周遭人也为我提供了一些思路,我会一点点整理 ...
- ZTOOLS HTTP®EXTEST&JSONS 工具包
下载地址:点击下载
- 流编辑器-sed
sed 参数: 1.'s' 替换 sed 's/search-word/replace-word/' file-name 替换file-name文件中的search-word为replace-word ...