在使用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)的更多相关文章

  1. 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) 遇到这个异常实属不小心所致,最初看到异出的错误信息时我误认为是 ...

  2. null id in entry (don't flush the Session after an exception occurs) 解决方法

    最近在学习基于ssh的注解的系统,然后在实现往数据库增加记录时可以增加第一个,第二个就报错,在网上查了很多资料,大多都是 该异常信息是在提示我们没有为数据中的非空字段设置值. 然后就一直没有明白 明明 ...

  3. org.hibernate.AssertionFailure: null id in xxx entry (don't flush the Session after an exception occurs)

    网上找了很久,发现造成原因有很多种,后来终于发现了端倪:看提示是发生了异常,查看业务代码,发现有这个逻辑:先插入记录,如果有唯一键约束异常(并发造成),catch时查询已存在的记录,查询的时候就报了此 ...

  4. org.hibernate.AssertionFailure: null id in com.you.model.User entry (don&#39;t flush the Session after a

    1.错误描写叙述 org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Sessio ...

  5. org.hibernate.AssertionFailure: null id in xxx.xx.xx的问题

    今日在开发时遇到一个比较奇怪的问题,保存时报这个异常: org.hibernate.AssertionFailure: null id in com.aa.TShoucang null id,这个是什 ...

  6. org.hibernate.AssertionFailure: null id 错误

    对象属性有Blob类型: 而Blob需在输入流中读取: InputStream in = new FileInputStream(url.getFile()); Blob bookPic = lobH ...

  7. org.hibernate.AssertionFailure: null id don't flus

    我的是字段编码和数据库不匹配,是爬的微博数据

  8. hibernate报错 java.lang.StackOverflowError: null

    在使用hibernate时,报错 java.lang.StackOverflowError: null 把当前线程的栈打满了 java.lang.StackOverflowError: null at ...

  9. activiti报错ProcessEngines.getDefaultProcessEngine()为null

    activiti报错ProcessEngines.getDefaultProcessEngine()为null 文件名错误,默认加载classpath下面的activiti.cfg.xml,而不是ac ...

随机推荐

  1. 好用的开源爬虫 jsoup

    中文Api http://www.open-open.com/jsoup/ 英文Api https://jsoup.org/

  2. jQuery.pager无刷新分页

    刚刚学习前端的时候,需要一个无刷新的分页功能,找了一个不错的,大家也有很大分享,在这里写一个自己的部分代码,前后端都有,需要的小伙伴可以参考一下,代码不是完整的. 直接上伪代码<样式代码省略,部 ...

  3. C# 属性和字段的区别

    属性和字段的区别 在C#中,我们可以非常自由的.毫无限制的访问公有字段, 但在一些场合中,我们可能希望限制只能给字段赋于某个范围的值.或是要求字段只能读或只能写, 或是在改变字段时能改变对象的其他一些 ...

  4. clip 属性剪裁绝对定位元素

    如果left >= right或者bottom <= top,则元素会被完全裁掉而不可见,即“隐藏”.通过这种方式隐藏的元素是可以被屏幕阅读器等辅助设备识别的,从而提高了页面的可用性. I ...

  5. C#知识体系(一) --- 常用的LInq 与lambda表达式

    LinQ是我们常用的技术之一.因为我们绕不开的要对数据进行一系列的调整,如 排序. 条件筛选.求和.分组.多表联接 等等. lambda则是我们常用的语法糖,配合linq使用天衣无缝,不知不觉就用上了 ...

  6. SAPCAR 压缩解压软件的使用方法

    SAPCAR 是 SAP 公司使用的压缩解压软件,从 SAP 网站下载的补丁包和小型软件基本都是扩展名为 car 或 sar 的,它们都可以用 SAPCAR 来解压.下面是它的使用说明: 用法: 创建 ...

  7. asp.net fileupload上传大文件时提示404.13错误

    IIS 7 默认文件上传大小时30M 要突破这个限制,需要做如下操作: 1. 修改IIS的applicationhost.config     打开 %windir%\system32\inetsrv ...

  8. Unity3d游戏场景优化杂谈(2)

    动态实时灯光相比静态灯光,非常耗费资源.所以除了能动的角色和物体(比如可以被打的到处乱飞的油桶)静态的地形和建筑,通通使用Lightmap. 强大的Unity内置了一个强大的光照图烘焙工具Beast, ...

  9. JS函数输出圆的半径和面积

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. libgcc_s.so.1 must be installed for pthread_cancel to work

    首先 whereis 看一下 有没有 libgcc_s.so.1 有的话 记得gcc --o xxx xxx.c -lpthread -lgcc_s