注册 登录
编程论坛 J2EE论坛

hibernate 建表成功数据库中无此表

lzy85712 发布于 2009-07-19 09:33, 1685 次点击
Fee的hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.

<hibernate-mapping>
    <class name="daomain.Fee" table="t_fee">
        <id name="id">
            <generator class="native"></generator>
        </id>
        <property name="feeid"/>
        <property name="feename"/>
        <property name="basefee"/>
        <property name="monthfee"/>
        <property name="describe"/>
    </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.
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/dianxing</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="show_sql">true</property>
        <mapping resource="daomain/Fee.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
package daomain;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {
    public static void main(String[] args) {

        Configuration cfg = new Configuration().configure();
        
        SchemaExport export = new SchemaExport(cfg);
        
        export.create(true, true);
    }
}
最后是控制台输出的语句
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
drop table if exists t_fee
create table t_fee (id integer not null auto_increment, feeid varchar(255), feename varchar(255), basefee double precision, monthfee double precision, describe varchar(255), primary key (id))
但是去数据库中找无t_fee表 库中还有一张t_user表
3 回复
#2
xia520qing2009-07-23 23:17
在加这样的一个属性
<property name="hbm2ddl.auto">create</property>
create|craete-drop|update|validate
create:
   每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
create-drop :
    每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
update:
    最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。
validate:
    每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。
#3
孤魂居士2009-08-06 14:54
事物提交没有
();
#4
jackeysion2009-08-10 18:26
应该是2楼所说的那样.将
<property name="hbm2ddl.auto">create</property>
create|craete-drop|update|validate
中间的属性create改为update或者干脆删掉这一个property
1