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是面向对象和属性的.
随机推荐
- C#联机获取公网IP
C#获取IP的方式有很多种,这里通过http://www.ipip.net/这个稳定的在线IP库的来获取公网IP. string tempip = "0.0.0.0"; WebRe ...
- Cruehead.1
查壳 没有 我拖 alt+F9 到上面 入口处 下断 关键跳 略过 就没了 要实现 强暴 直接过... 仔细来看看... 那两个调用 都下断 看看 判断 ...
- SharePoint REST Create Folder
function createListFolder(siteUrl, listName, foldername) { var serverUrl = _spPageContextInfo.webAbs ...
- MVC 知识点学习3(linq to sql)
1.通过DbContext对象的Database.SqlQuery执行sql语句 string query = "SELECT EnrollmentDate, COUNT(*) AS Stu ...
- ftpget 从Windows FTP服务端获取文件
/********************************************************************************* * ftpget 从Windows ...
- Java用户线程和守护线程
今天看Java一个关于多线程返回值方式的示例,发现一个自己不太能理解的问题,就是在主线程中启动了几个工作线程,主线程中也没有join,工作线程居然也是正常输出了回调的结果.这个跟linux C++下的 ...
- Decorator实现AOP编程。
Program.cs class Program { static void Main(string[] args) { User user = " }; var processor = T ...
- 如何制作快速加载的HTML页面
整理出来的tip 减小页面的大小 在页面下载中,页面的大小至今扮演着非常重要的因素. 减小页面的大小能够通过排除不必要空格,注释,动态内嵌脚本,和放入外部文件的 CSS 等在页面结构中很小的改变都能够 ...
- tabelView右滑选择进行删除
如何使用UITableViewRowAction实现右滑选择呢? 1.在iOS8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在iOS8中系统已经写好了,只 ...
- linux5个搜索命令
概要 linux中主要有5个文件查找命令:find.locate.whereis.which.type.find最为强大,但耗时较长.locate可看做find的精简版,但是它的速度非常快.where ...