运行程序的结果是:Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [lee.News];
Caused by: java.sql.SQLException: Table 'hibernates.news_table' doesn't exist
奇怪的是数据没有插入成功,反而自动把表给删除了。
纠结了好长时间,请懂的朋友支援,谢谢!!mysql:
create table news_table (
news_id int primary key auto_increment,
news_title varchar(20) not null,
content varchar(50) not null );News.java :
package lee;
public class News {
private Integer id;
private String title;
private String content;
public News() {}
public News(Integer id, String title, String content) {
this.setId(id);
this.setTitle(title);
this.setContent(content);
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}Test.java :
package lee;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;public class Test {
public static void main(String[] args) throws Exception
{
//实例化Configuration
Configuration conf = new Configuration().configure();
//实例化SessionFactory
SessionFactory sf = conf.buildSessionFactory();
//实例化Session
Session sess = sf.openSession();
//开始事务
Transaction tx = sess.beginTransaction();
//创建消息实例
News n = new News();
//设置消息标题和消息内容
n.setTitle("疯狂Java联盟成立了");
n.setContent("疯狂Java联盟成立了,网址是www.crazyit.org");
//保存消息
sess.save(n);
//提交事务
tx.commit();
//关闭Session
sess.close();
}
}News.hbm.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="lee">
<!-- 每个class元素对应一个持久化对象 -->
<class name="News" table="news_table">
<!-- id元素定义持久化类的标识属性 -->
<id name="id" column="news_id" unsaved-value="null">
<generator class="identity"/>
</id>
<!-- property元素定义常规属性 -->
<property name="title" column="news_title" type="string"/>
<property name="content" type="string"/>
</class>
</hibernate-mapping>hibernate.cfg.xml :
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 指定连接数据库所用的驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 指定连接数据库的url,hibernate是连接的数据库名 -->
<property name="connection.url">jdbc:mysql://localhost/hibernates</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password">901110</property>
<!-- 使用C3P0连接池,指定连接池的相关配置信息 -->
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 根据需要自动创建数据库 -->
<property name="hbm2ddl.auto">create</property>
<!-- 罗列所有的映射文件-->
<mapping resource="News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Caused by: java.sql.SQLException: Table 'hibernates.news_table' doesn't exist
奇怪的是数据没有插入成功,反而自动把表给删除了。
纠结了好长时间,请懂的朋友支援,谢谢!!mysql:
create table news_table (
news_id int primary key auto_increment,
news_title varchar(20) not null,
content varchar(50) not null );News.java :
package lee;
public class News {
private Integer id;
private String title;
private String content;
public News() {}
public News(Integer id, String title, String content) {
this.setId(id);
this.setTitle(title);
this.setContent(content);
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}Test.java :
package lee;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;public class Test {
public static void main(String[] args) throws Exception
{
//实例化Configuration
Configuration conf = new Configuration().configure();
//实例化SessionFactory
SessionFactory sf = conf.buildSessionFactory();
//实例化Session
Session sess = sf.openSession();
//开始事务
Transaction tx = sess.beginTransaction();
//创建消息实例
News n = new News();
//设置消息标题和消息内容
n.setTitle("疯狂Java联盟成立了");
n.setContent("疯狂Java联盟成立了,网址是www.crazyit.org");
//保存消息
sess.save(n);
//提交事务
tx.commit();
//关闭Session
sess.close();
}
}News.hbm.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="lee">
<!-- 每个class元素对应一个持久化对象 -->
<class name="News" table="news_table">
<!-- id元素定义持久化类的标识属性 -->
<id name="id" column="news_id" unsaved-value="null">
<generator class="identity"/>
</id>
<!-- property元素定义常规属性 -->
<property name="title" column="news_title" type="string"/>
<property name="content" type="string"/>
</class>
</hibernate-mapping>hibernate.cfg.xml :
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 指定连接数据库所用的驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 指定连接数据库的url,hibernate是连接的数据库名 -->
<property name="connection.url">jdbc:mysql://localhost/hibernates</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password">901110</property>
<!-- 使用C3P0连接池,指定连接池的相关配置信息 -->
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 根据需要自动创建数据库 -->
<property name="hbm2ddl.auto">create</property>
<!-- 罗列所有的映射文件-->
<mapping resource="News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
表不存在啊
你先去数据库确定一下 这张表在不在
如果在的话 把sql语句打印出来 手动插入到表里边 看看能否插成功。如果能插入成功那就是你代码的问题了!
改成update试试
<property name="hbm2ddl.auto">create</property>
一看就知道这句错了 create是每次吧原来的表删了重建
<property name="hibernate.hbm2ddl.auto">update</property> update:表示自动根据model对象来更新表结构,启动hibernate时会自动检查数据库,如果缺少表,则自动建表;如果表里缺少列,则自动添加列。 还有其他的参数:
create:启动hibernate时,自动删除原来的表,新建所有的表,所以每次启动后的以前数据都会丢失。 create-drop:启动hibernate时,自动创建表,程序关闭时,自动把相应的表都删除。所以程序结束时,表和数据也不会再存在。 PS:数据库要预先建立好,因为hibernate只会建表,不会建库 ==========================================
表结构和数据总是在程序执行的时候无端的修改,折腾了好长时间,查了很长时间hibernate的数据库映射文件和接口程序,始终没有发现有什么错误,到最后才发现了它!
<property name="hibernate.hbm2ddl.auto" value="update" />
解释如下:hibernate.hbm2ddl.auto Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. eg. validate | update | create | create-drop其实这个参数的作用主要用于:自动创建|更新|验证数据库表结构。
如果没有此方面的需求建议set value="none".其它几个参数的意思:validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构如果发现数据库表丢失或新增,请检查hibernate.hbm2ddl.auto的配置 可设置 <property name="hibernate.hbm2ddl.auto" value="none" />
这个出问题了,你这样写,每次执行都会重新建表
值改成update就行了,create是会在hibernate每次运行时重新建各个表,同理,update是每次更新表。看看文档