在使用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對象的創建。具體原因還需要深究。