这两天一直因为自动建表的问题弄的头疼,一直没有解决,现将有关思想和代码发上来,请各位大侠帮忙分析一下。
A:第一种思想
   通过在hibernate配置文件中进行相应的配置,并做相应的映射,pojo类及相关配置如下:
   1、POJO类public class UserInfo {
String id;
String userId;
String password;
String userName;
String belongShop;
String isAdmin;  //超级管理员,具有所有店管理权限
String isSubAdmin;//本店管理员,具有本店所有管理权限
String isCommon;//通用账号,可查看所有店基本情况,但不可查看详情

String sex;
String birthday;
String phone;
String userAddress;
String education;//学历
String professional;//专业
String regDate;//注册日期
String memo;
         ……
         //set与get方法省略
}     2、实体类与对应表的映射文件(beauty-base.cfg.xml)<hibernate-mapping package="com.beyond.po">
   <class name="UserInfo" table="USER_INFO">
       <id name="id" type="string" column="ID">
<generator class="identity"/>
</id>
        <property name="userId" column="USER_ID" type="string" not-null="true" length="20"/>
        <property name="userName" column="USER_Name" type="string" not-null="false" length="50"/> 
        <property name="password" column="PASSWORD" type="string" not-null="true" length="20"/>    
        <property name="belongShop" column="BELONG_SHOP" type="string" not-null="false" length="20"/>           
        <property name="isAdmin" column="IS_ADMIN" type="string" not-null="false" length="2"/>           
        <property name="isSubAdmin" column="IS_SUB_ADMIN" type="string" not-null="false" length="2"/>           
        <property name="isCommon" column="IS_COMMON" type="string" not-null="false" length="2"/>                   
        <property name="sex" column="SEX" type="string" not-null="false" length="1"/>                   
        <property name="birthday" column="BIRTHDAY" type="string" not-null="false" length="20"/>                   
        <property name="phone" column="PHONE" type="string" not-null="false" length="20"/>                   
        <property name="userAddress" column="USER_ADDRESS" type="string" not-null="false" length="200"/>                   
        <property name="education" column="EDUCATION" type="string" not-null="false" length="20"/>                   
        <property name="professional" column="PROFESSIONAL" type="string" not-null="false" length="50"/>                   
        <property name="regDate" column="REG_DATE" type="string" not-null="false" length="20"/>  
        <property name="memo" column="MEMO" type="string" not-null="false" length="2000"/>                                                   
   </class>
</hibernate-mapping>
 
        3、hibernate.cfg.xml中的相应配置<session-factory>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JADDB</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="myeclipse.connection.profile">MySQLServer</property>    <property name="connection.pool_size">1</property>
    <property name="current_session_context_class">thread</property>    
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>


<mapping resource="com/beyond/po/beauty-base.cfg.xml" /></session-factory>    按上述方法进行后,重启tomcat容器,无任何反应,表既没有被创建,也不报任何错误
B:第二种思想
      根据一些朋友的建议,通过写servlet的方法实现,servlet关键代码如下: public void init(){
try {   
Configuration cfg=new Configuration().configure("/hibernate.cfg.xml");
SchemaExport export=new SchemaExport(cfg);
export.create(true,true);
} catch (Exception e) {
e.printStackTrace();
} }
      并在web.xml中进行servlet配置
  <servlet>
      <servlet-name>hiber</servlet-name>
      <servlet-class>com.beyond.ORM.InitHibernate</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>   重启服务器后,控制台可以看到输出的创建表的相关代码(见如下所示),但查看数据库表,并没有发现表被真正的建立起,很是迷惑。
drop table USER_INFO
create table USER_INFO (ID varchar(255) identity not null, USER_ID varchar(20) n
ot null, USER_Name varchar(50) null, PASSWORD varchar(20) not null, BELONG_SHOP
varchar(20) null, IS_ADMIN varchar(2) null, IS_SUB_ADMIN varchar(2) null, IS_COM
MON varchar(2) null, SEX varchar(1) null, BIRTHDAY varchar(20) null, PHONE varch
ar(20) null, USER_ADDRESS varchar(200) null, EDUCATION varchar(20) null, PROFESS
IONAL varchar(50) null, REG_DATE varchar(20) null, MEMO varchar(2000) null, prim
ary key (ID))两种方法自动建表都没有成功,不知道问题是出在哪里,请大家帮我分析一下,给个回复,谢谢!

解决方案 »

  1.   

    数据库是手动建立好了的,数据库为SQLServer 2000
      

  2.   

    <property name="hbm2ddl.auto">update</property>
    改为
    <property name="hbm2ddl.auto">create</property>
      

  3.   

    update好些..
    update是看有没有表. 如果没有表就新建, 有表就更新.! create是每次都建, 那数据不就丢了?
      

  4.   

    public class ExportDB {
        public static void main(String[] args){
            //读取hibernate.cfg.xml配置文件
            Configuration config = new Configuration().configure();
            //把对象类导成表
            SchemaExport export = new SchemaExport(config);
            //生成ddl,表的创建
            export.create(true, true);
        }
    }