报错: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 ...
随机推荐
- WCF Restful 服务器配置文件
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSec ...
- NOIP2016 模拟赛
7.10 T1:求出一个矩阵中平均数大于0的子矩阵的最大面积. T2:给出一个N行的,第I行有n+1-i的倒三角形,从中选取m个数,只有当前数的左上角和右上角都被选是才能选当前数,求选的数字的最大和 ...
- DataTable,DataGridVIew转换到xls 方法 (转)
private void dataTableToCsv(DataTable table, string file) { string title = ""; FileStream ...
- 一键编译go文件命令.bat
一键编译go文件命令.bat , 请新建 一键编译go文件命令.bat 文件,放到你的xxx.go文件目录下 ( 欢迎加入go语言群: 218160862 , 群内有实践) 点击加入 @e ...
- Foundation
类:NSObject .NSString.NSMutableString.NSNumber.NSValue.NSDate.NSDateFormatter.NSRange.Collections:NSS ...
- NPOI格式设置1
using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; //创建Execl IWorkbook hssfworkbook =new HSSFWorkbo ...
- android实现控制视频播放次数
android实现控制视频播放次数,实质就是每个视频片段播放完后,通过MediaPlayer设置监听器setOnCompletionListener监听视频播放完毕,用Handler发送消息再次激活视 ...
- XmlSerializer(Type type, Type[] extraTypes) 内存泄漏
在使用XmlSerializer进行序列化或者反序列的时候,对于下面的两个构造方法 XmlSerializer(Type)XmlSerializer.XmlSerializer(Type, Strin ...
- JAVA hashmap知识整理
HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是否可以随机应变使用多种思路解决问题.HashMap的工作原理.ArrayList与Vect ...
- 【HDU1257】最少拦截系统(贪心)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...