请问各位为什么Hibernate中的属性<property name="hbm2ddl.auto">update</property>没有用啊,不是说hbm2ddl.auto的值改为update就可以改变表的结构吗?
我的hibernate配置为
<session-factory>        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibnate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
         
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
        
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        
         
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>
        
        <!--  <mapping resource="com/bjsxt/hibnate/model/student.hbm.xml"/>-->
        <mapping class="com.bjsxt.hibnate.model.teacher"/>
    </session-factory></hibernate-configuration>
我的数据库名字叫hibnate,表名叫teacher,表中初始的有3个属性id,name,age类为
@Entity
public class teacher { int age;
int id ;
String name;

public int getAge() {
       return age;
}
@Id
public int getId() {
return id;
}

public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}

} 执行类为
package com.bjsxt.hibnate.model;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.Test;
public class teachertest { public static void main(String args[]){

    teacher t=new teacher();
    t.setId(1);
    t.setName("第而");
    t.setAge(2);
   
AnnotationConfiguration cfg=new AnnotationConfiguration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}
如果我要是为teacher类增加一个字段就会报错
例如
@Entity
public class teacher { int age;
int id ;
String name;
String tian1;     //增加了一个字段 tian1


public String getTian1() {
return tian1;
}
public void setTian1(String tian1) {
this.tian1 = tian1;
}
public int getAge() {
return age;
}
@Id
public int getId() {
return id;
}

public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}

} hibernate报错Unknown column 'tian1' in 'field list'
小弟很急啊,弄了一天了,各位帮忙看看啊,小弟不胜感激

解决方案 »

  1.   

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

  2.   

    4个属性分别是
    create:每次启动时创建表结构,在建表前会先执行删除表的语句,以保证创建成功。
    update:每次启动时检测一下,如果表结构没有,则创建,不一样,则更新。
    create-drop: 每次启动时创建表结构,退出前删除表结构 
    validate: 每次启动时检测一下,如果表结构不一样,就报错hbm2ddl.auto ,一般在新增时有效,更新时无效。
      

  3.   

    com/bjsxt/hibnate/model/student.hbm.xml这个配置文件内容呢