zoukankan      html  css  js  c++  java
  • 报错: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:

     1 package com.tt.hibernate.entities;
     2 
     3 import java.io.FileInputStream;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 import java.sql.Blob;
     7 import java.sql.Connection;
     8 import java.sql.SQLException;
     9 import java.util.Date;
    10 
    11 import org.hibernate.Hibernate;
    12 import org.hibernate.Session;
    13 import org.hibernate.SessionFactory;
    14 import org.hibernate.Transaction;
    15 import org.hibernate.cfg.Configuration;
    16 import org.hibernate.jdbc.Work;
    17 import org.hibernate.service.ServiceRegistry;
    18 import org.hibernate.service.ServiceRegistryBuilder;
    19 import org.junit.After;
    20 import org.junit.Before;
    21 import org.junit.Test;
    22 
    23 public class HibernateTest {
    24 
    25     private SessionFactory sessionFactory;
    26     private Session session;
    27     private Transaction transaction;
    28     
    29     @Before
    30     public void init(){
    31         System.out.println("init...");
    32         
    33         Configuration configuration = new Configuration().configure();
    34         ServiceRegistry serviceRegistry = 
    35                 new ServiceRegistryBuilder().applySettings(configuration.getProperties())
    36                                             .buildServiceRegistry();
    37         sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    38         
    39         session = sessionFactory.openSession();
    40         
    41         transaction = session.beginTransaction();
    42     }
    43     
    44     @After
    45     public void destroy() {
    46         System.out.println("destroy...");
    47         
    48         transaction.commit();
    49         session.close();
    50         sessionFactory.close();
    51     }
    68     
    69     @Test
    70     public void testBlob() throws IOException, SQLException{
    71         News news = new News();
    72         
    73         news.setAuthor("cc");
    74         news.setTitle("CC");
    75         news.setDesc("DESC");
    76         news.setContent("CONTENT");
    77         news.setDate(new Date());
    78         
    79         InputStream stream = new FileInputStream("SHQ.jpg");
    80         Blob image = Hibernate.getLobCreator(session)
    81                               .createBlob(stream, stream.available());
    82         
    83         news.setImage(image);
    84         
    85         session.save(news);
    92     }

    Hibernate.cfg.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
     5 <hibernate-configuration>
     6     <session-factory>
     7     
     8         <!-- Hibernate连接数据库的基本信息 -->
     9         <property name="conncection.username">root</property>
    10         <property name="connection.password">1234</property>
    11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    12         <property name="connection.url">jdbc:mysql://localhost:3306/Hibernate</property>
    13         
    14         <!-- Hibernate的基本配置 -->
    15         
    16         <!-- Hibernate使用的数据库方言 -->
    17         <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    18         
    19         <!-- 运行时是否打印SQL -->
    20         <property name="show_sql">true</property>
    21         
    22         <!-- 运行时是否格式化SQL -->
    23         <property name="format_sql">true</property>
    24         
    25         <!-- 生成数据表的策略 -->
    26         <property name="hbm2ddl.auto">create</property>
    27         
    28         <!-- 设置Hibernate的事务隔离级别 :2代表读已提交-->
    29         <property name="connection.isolation">2</property>
    30         
    31         <!-- 删除对象后,使其OID设置为null -->
    32         <property name="use_identifier_rollback">true</property>      
    33         
    34         <!-- 配置C3P0数据源 -->
    35         <property name="hibernate.c3p0.max_size">10</property>
    36         <property name="hibernate.c3p0.min_size">5</property>
    37         <property name="hibernate.c3p0.acquire_increment">2</property>
    38         
    39         <property name="hibernate.c3p0.idle_test_period">2000</property>
    40         <property name="hibernate.c3p0.timeout">2000</property>
    41         
    42         <property name="hibernate.c3p0.max_statements">10</property>
    43         
    44         <!-- 设定JDBC的statement读取数据的时候每次从数据库中取出的记录条数 -->
    45         <property name="hibernate.jdbc.fetch_size">100</property>
    46         
    47         <!-- 设定对数据库进行批量删除,批量更新和批量插入的时候的批次大小 -->
    48         <property name="jdbc.batch_size">30</property>
    49          
    50         <!-- 需要关联的hibernate映射文件 .jbm.xml -->
    51         <mapping resource="com/tt/hibernate/entities/News.hbm.xml"/>
    52         <!--  <mapping resource="com/tt/hibernate/entities/Worker.hbm.xml"/>-->
    53         
    54     </session-factory>
    55 </hibernate-configuration>

    News.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 <!-- Generated 2016-12-25 12:12:49 by Hibernate Tools 3.5.0.Final -->
     5 <hibernate-mapping package="com.tt.hibernate.entities">
     6     <class name="News" table="NEWS">
     7         <id name="id" type="java.lang.Integer">
     8             <column name="ID" />
     9             <generator class="native" />
    10         </id>
    11         <property name="title" type="java.lang.String">
    12             <column name="TITLE" />
    13         </property>
    14         <property name="author" type="java.lang.String">
    15             <column name="AUTHOR" />
    16         </property>
    17         <property name="date" type="java.util.Date">
    18             <column name="DATE" />
    19         </property>
    20         <property name="desc" type="java.lang.String">
    21             <column name="DESC" />
    22         </property>
    23         <property name="content" type="java.lang.String">
    24             <column name="CONTENT" />
    25         </property>
    26         <property name="image" type="java.sql.Blob">
    27             <column name="IMAGE" />
    28         </property>
    29     </class>
    30 </hibernate-mapping>

    根据报错信息,定位到SQL语句的第6行:Content varchar(255)

     症状分析:

    如果将News.hbm.xml文件里的desc属性设置改为下面的映射派生属性,运行正常而且可以看到news表格里是没有DESC这一列。

    那么为什么会出现这样的现象?为什么desc只能作为派生属性存在呢?它和title、author和date存在什么区别?唯一的区别 后三者作为参数参与了new News对象的创建。具体原因还需要深究。

  • 相关阅读:
    macOS 上配置 Lua
    Oracle.ManagedDataAccess.dll
    offer
    Costura.Fody
    日志系统
    实战框架ABP
    什么是算法?
    HTTP状态码->HTTP Status Code
    How to untar a TAR file using Apache Commons
    python实践3:cursor() — 数据库连接操作
  • 原文地址:https://www.cnblogs.com/TTTTT/p/6228970.html
Copyright © 2011-2022 走看看