package com.test.bean.test;import java.io.File;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;public class ExportDB { static Session session; /**
* @param args
*/
public static void main(String[] args){
Configuration config = null;
Transaction tx = null;
try {
config = new Configuration().configure("WEB-INF/applicationContext.xml");
System.out.println("Creating tables...");
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
System.out.println("Table created.");
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
} catch (HibernateException e){
e.printStackTrace();
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
} finally {
}
}
}我的目的:从csdn论坛下载了一个北风网的项目(主要想学习extjs),项目是完整的但是没有数据库,这里我想手工补上数据库。于是就用hibernate的反向生成数据库功能:
这里我读的是spring的配置文件,控制台报错说路径没找到(路径对)。上网看了下被spring容器管理的Hibernate说是还需要配置什么属性?(如果想反向生成数据库表的话)。 求大虾教我咋生成数据库表,越详细越感谢。hibernate数据库spring反向生成
log4j:WARN Please initialize the log4j system properly.
org.hibernate.HibernateException: WEB-INF/applicationContext.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1424)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1446)
at com.test.bean.test.ExportDB.main(ExportDB.java:23)
提供下spring配置文件(不知道是不是这里有问题):
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/pfms</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>woyi</value>
</property>
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>User.hbm.xml</value>
<value>com/ibeifeng/po/PayOut.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
org.hibernate.MappingException: invalid configuration 这就是配置文件的错误了。
我之前那是读取Hibernate配置文件方法,不过我也没有用这个方法(麻烦),用了另外一个方法:
在applicationContext.xml(sessionFoacory)中配置:<prop key="hibernate.hbm2ddl.auto">update</prop>
加这么一句,部署,访问,然后打开数据库一看,竟然生成了!神奇(神仙都觉得奇怪)。