报错: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 ...
随机推荐
- JAVAWEB学习总结 HttpServletResponse对象(一)
Web服务器收到客户端(浏览器)的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象,和代表响应的response对象. request和response对象既然代表请求和响 ...
- LayaAir引擎——(二)
LayaAir引擎 -> 工具 -> 图集打包例子
- vfp 智能感知拓展应用
*======================================================================================== * * Versio ...
- linux sed的使用
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理, 可以将数据行进行替换.删除.新增.选取等特定工作. sed本质上是一个编辑器,但是它是非交互式的,这点与VIM不同:同时 ...
- Chp11 11.7
<Java语言程序设计>P327 题目要求使用数组来模拟实现ArrayList的一些方法,并要求可以根据实际长度来实现数组自动增长,这里只贴出LikeArrayList.java 测试方法 ...
- java SHA1WithRSA 算法
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStrea ...
- Socket实现粗略的Android聊天功能
面试时写过,但是很乱,今天看到代码库里有一个,还是很规范的,贴出来. 主要用到俩类,一个是ServerSocket.class 一个是Socket.class. 记得要加权限: <uses-p ...
- jquery+ajax跨域请求webservice
最近几天在学习webservice...在学习的时候便想到用ajax的方式去请求webservice.. 一直在测试..如果这个被请求的webservice和自己使用的是同一个端口号.则不用考虑那aj ...
- HTML DOM 节点
一切皆节点 在 HTML DOM (文档对象模型)中,节点主要包括(括号中用数字表示节点类型):元素(1).属性(2).文本(3,其中换行符也是一个文本节点).注释(8).文档(9). 其中重要的方法 ...
- aes rsa加密
aes在加密时,若加密字符串的长度不是16,则会在后面加0x00补足16位,所以在解密后还应该去除0x00 小于16字节的原文会得到16字节长度的密文,小于32字节的原文会得到32字节长度的密文,大于 ...