Hibernate创建数据库表无效解决方案
Hibernate创建数据库表无效
持久化类
hibernate.cfg.xml
对象关系映射文件
创建数据库表
package lee;
import org.crazyit.app.domain.News;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class NewsManager {
public static void main(String[] args)
{
//读取配置文件hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
//创建SchemaExport实例;
SchemaExport sExport = new SchemaExport(cfg);
//创建数据表项
sExport.create(true, true);
}
}
运行后没有报错只有log4j报了两个警告,但是MySQL并没有创建数据库表

------解决方案--------------------
一般都自己去建立数据库吧,表可以通过这样自动生成
------解决方案--------------------
目测楼主c3p0配置有点问题,还要一个属性
持久化类
package org.crazyit.app.domain;
public class News {
private Integer id;
private String title;
private String 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;
}
}
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>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<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.validata">true</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="org/crazyit/app/domain/News.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
对象关系映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.crazyit.app.domain">
<class name="News" table="news_table">
<id name="id">
<generator class="identity"></generator>
</id>
<property name="title"></property>
<property name="content"></property>
</class>
</hibernate-mapping>
创建数据库表
package lee;
import org.crazyit.app.domain.News;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class NewsManager {
public static void main(String[] args)
{
//读取配置文件hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
//创建SchemaExport实例;
SchemaExport sExport = new SchemaExport(cfg);
//创建数据表项
sExport.create(true, true);
}
}
运行后没有报错只有log4j报了两个警告,但是MySQL并没有创建数据库表
------解决方案--------------------
一般都自己去建立数据库吧,表可以通过这样自动生成
------解决方案--------------------
目测楼主c3p0配置有点问题,还要一个属性
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>,没有指定hibernate.connection.provider_class,hibernate会使用默认的连接池