报错: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 perform
ERROR: HHH000389: Unsuccessful: create table NEWS (ID integer not null auto_increment, TITLE varchar(255), AUTHOR varchar(255), DATE datetime, DESC varchar(255), CONTENT varchar(255), IMAGE longblob, primary key (ID)) ENGINE=InnoDB
十二月 28, 2016 10:17:02 上午 org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC varchar(255),
CONTENT varchar(255),
IMAGE longblob,
' at line 6
十二月 28, 2016 10:17:02 上午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate:
create table NEWS (
ID integer not null auto_increment,
TITLE varchar(255),
AUTHOR varchar(255),
DATE datetime,
DESC varchar(255),
CONTENT varchar(255),
IMAGE longblob,
primary key (ID)
) ENGINE=InnoDB
Hibernate:
insert
into
NEWS
(TITLE, AUTHOR, DATE, DESC, CONTENT, IMAGE)
values
(?, ?, ?, ?, ?, ?)
十二月 28, 2016 10:17:03 上午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1064, SQLState: 42000
十二月 28, 2016 10:17:03 上午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC, CONTENT, IMAGE) values ('CC', 'cc', '2016-12-28 10:17:02.785', 'DESC', 'CO' at line 1
destroy...
十二月 28, 2016 10:17:03 上午 org.hibernate.AssertionFailure <init>
ERROR: HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in com.tt.hibernate.entities.News entry (don't flush the Session after an exception occurs)
各部分代码如下:
HibernateTest.java:
package com.tt.hibernate.entities; import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date; import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class HibernateTest { private SessionFactory sessionFactory;
private Session session;
private Transaction transaction; @Before
public void init(){
System.out.println("init..."); Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry); session = sessionFactory.openSession(); transaction = session.beginTransaction();
} @After
public void destroy() {
System.out.println("destroy..."); transaction.commit();
session.close();
sessionFactory.close();
} @Test
public void testBlob() throws IOException, SQLException{
News news = new News(); news.setAuthor("cc");
news.setTitle("CC");
news.setDesc("DESC");
news.setContent("CONTENT");
news.setDate(new Date()); InputStream stream = new FileInputStream("SHQ.jpg");
Blob image = Hibernate.getLobCreator(session)
.createBlob(stream, stream.available()); news.setImage(image); session.save(news);
}
Hibernate.cfg.xml
<?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> <!-- Hibernate连接数据库的基本信息 -->
<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="show_sql">true</property> <!-- 运行时是否格式化SQL -->
<property name="format_sql">true</property> <!-- 生成数据表的策略 -->
<property name="hbm2ddl.auto">create</property> <!-- 设置Hibernate的事务隔离级别 :2代表读已提交-->
<property name="connection.isolation">2</property> <!-- 删除对象后,使其OID设置为null -->
<property name="use_identifier_rollback">true</property> <!-- 配置C3P0数据源 -->
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.acquire_increment">2</property> <property name="hibernate.c3p0.idle_test_period">2000</property>
<property name="hibernate.c3p0.timeout">2000</property> <property name="hibernate.c3p0.max_statements">10</property> <!-- 设定JDBC的statement读取数据的时候每次从数据库中取出的记录条数 -->
<property name="hibernate.jdbc.fetch_size">100</property> <!-- 设定对数据库进行批量删除,批量更新和批量插入的时候的批次大小 -->
<property name="jdbc.batch_size">30</property> <!-- 需要关联的hibernate映射文件 .jbm.xml -->
<mapping resource="com/tt/hibernate/entities/News.hbm.xml"/>
<!-- <mapping resource="com/tt/hibernate/entities/Worker.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-12-25 12:12:49 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping package="com.tt.hibernate.entities">
<class name="News" table="NEWS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<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>
<property name="desc" type="java.lang.String">
<column name="DESC" />
</property>
<property name="content" type="java.lang.String">
<column name="CONTENT" />
</property>
<property name="image" type="java.sql.Blob">
<column name="IMAGE" />
</property>
</class>
</hibernate-mapping>
根据报错信息,定位到SQL语句的第6行:Content varchar(255)
症状分析:
如果将News.hbm.xml文件里的desc属性设置改为下面的映射派生属性,运行正常而且可以看到news表格里是没有DESC这一列。
那么为什么会出现这样的现象?为什么desc只能作为派生属性存在呢?它和title、author和date存在什么区别?唯一的区别 后三者作为参数参与了new News对象的创建。具体原因还需要深究。
报错:org.hibernate.AssertionFailure: null id in com.tt.hibernate.entities.News entry (don't flush the Session after an exception occurs)的更多相关文章
- null id in entry (don't flush the Session after an exception occurs)
null id in entry (don't flush the Session after an exception occurs) 遇到这个异常实属不小心所致,最初看到异出的错误信息时我误认为是 ...
- null id in entry (don't flush the Session after an exception occurs) 解决方法
最近在学习基于ssh的注解的系统,然后在实现往数据库增加记录时可以增加第一个,第二个就报错,在网上查了很多资料,大多都是 该异常信息是在提示我们没有为数据中的非空字段设置值. 然后就一直没有明白 明明 ...
- org.hibernate.AssertionFailure: null id in xxx entry (don't flush the Session after an exception occurs)
网上找了很久,发现造成原因有很多种,后来终于发现了端倪:看提示是发生了异常,查看业务代码,发现有这个逻辑:先插入记录,如果有唯一键约束异常(并发造成),catch时查询已存在的记录,查询的时候就报了此 ...
- org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Session after a
1.错误描写叙述 org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Sessio ...
- org.hibernate.AssertionFailure: null id in xxx.xx.xx的问题
今日在开发时遇到一个比较奇怪的问题,保存时报这个异常: org.hibernate.AssertionFailure: null id in com.aa.TShoucang null id,这个是什 ...
- org.hibernate.AssertionFailure: null id 错误
对象属性有Blob类型: 而Blob需在输入流中读取: InputStream in = new FileInputStream(url.getFile()); Blob bookPic = lobH ...
- org.hibernate.AssertionFailure: null id don't flus
我的是字段编码和数据库不匹配,是爬的微博数据
- hibernate报错 java.lang.StackOverflowError: null
在使用hibernate时,报错 java.lang.StackOverflowError: null 把当前线程的栈打满了 java.lang.StackOverflowError: null at ...
- activiti报错ProcessEngines.getDefaultProcessEngine()为null
activiti报错ProcessEngines.getDefaultProcessEngine()为null 文件名错误,默认加载classpath下面的activiti.cfg.xml,而不是ac ...
随机推荐
- 配置apache apache服务器如何配置多站点
http://jingyan.baidu.com/article/5225f26b07605be6fa090890.html 让Apache在启动时能加载虚拟主机模块. 打开Apache安装目录下co ...
- kafka性能基准测试
转载请注明出处:http://www.cnblogs.com/xiaodf/ 1.测试环境 该benchmark用到了六台机器,机器配置如下 l IntelXeon 2.5 GHz processo ...
- HDU 4857 逃生 (反向拓扑排序 & 容器实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.
-- :::] This application is modifying the autolayout engine from a background thread, which can lead ...
- js跳转传递参数
额,利用j获取了GridView中选中行数据后,通过JavaScript做跳转,传递参数的时候发现,当参数有中文的时候就会乱码, 当然出现这种情况的时候就需要对跳转的url进行编码 var urlX ...
- akka实现的actor
定义一个 Actor 类 要定义自己的Actor类,需要继承 Actor 并实现receive 方法. receive 方法需要定义一系列 case 语句(类型为 PartialFunction[An ...
- 创建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 这是因为你 ...
- js中属性和方法的类型和区别
对象的属性:私有属性(var).类属性(静态属性).对象属性(this).原型属性(prototype). 对象的方法: 私有方法(funtion).类方法(静态方法).对象方法(this).原型方法 ...
- SpringMVC,3种不同的URL路由配置方法
1. 先说说一种比较常见的: <servlet> <servlet-name>theDispatcher</servlet-name> <servlet-cl ...
- 模仿console自写函数打印js的对象
本以为写个递归函数就可以将js的对象打印出来. 当然第一个想到的估计是JSON.stringify() 这个函数.但这个函数打印到浏览器 显示效果不友好.最友好的显示肯定是 控制台打印咯. 结果尝试打 ...