问题描述
请问各位为什么Hibernate中的属性<propertyname="hbm2ddl.auto">update</property>没有用啊,不是说hbm2ddl.auto的值改为update就可以改变表的结构吗?我的hibernate配置为<session-factory><!--Databaseconnectionsettings--><propertyname="connection.driver_class">com.mysql.jdbc.Driver</property><propertyname="connection.url">jdbc:mysql://localhost/hibnate</property><propertyname="connection.username">root</property><propertyname="connection.password">root</property><!--JDBCconnectionpool(usethebuilt-in)--><propertyname="connection.pool_size">1</property><!--SQLdialect--><propertyname="dialect">org.hibernate.dialect.HSQLDialect</property><!--EnableHibernate'sautomaticsessioncontextmanagement--><propertyname="current_session_context_class">thread</property><!--Disablethesecond-levelcache--><propertyname="cache.provider_class">org.hibernate.cache.NoCacheProvider</property><!--EchoallexecutedSQLtostdout--><propertyname="show_sql">true</property><!--Dropandre-createthedatabaseschemaonstartup--><propertyname="hbm2ddl.auto">update</property><!--<mappingresource="com/bjsxt/hibnate/model/student.hbm.xml"/>--><mappingclass="com.bjsxt.hibnate.model.teacher"/></session-factory></hibernate-configuration>我的数据库名字叫hibnate,表名叫teacher,表中初始的有3个属性id,name,age类为@Entitypublicclassteacher{intage;intid;Stringname;publicintgetAge(){returnage;}@IdpublicintgetId(){returnid;}publicStringgetName(){returnname;}publicvoidsetAge(intage){this.age=age;}publicvoidsetId(intid){this.id=id;}publicvoidsetName(Stringname){this.name=name;}}执行类为packagecom.bjsxt.hibnate.model;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.cfg.AnnotationConfiguration;importorg.junit.Test;publicclassteachertest{publicstaticvoidmain(Stringargs[]){teachert=newteacher();t.setId(1);t.setName("第而");t.setAge(2);AnnotationConfigurationcfg=newAnnotationConfiguration();SessionFactorysf=cfg.configure().buildSessionFactory();Sessionsession=sf.openSession();session.beginTransaction();session.save(t);session.getTransaction().commit();session.close();sf.close();}}如果我要是为teacher类增加一个字段就会报错例如@Entitypublicclassteacher{intage;intid;Stringname;Stringtian1;//增加了一个字段tian1publicStringgetTian1(){returntian1;}publicvoidsetTian1(Stringtian1){this.tian1=tian1;}publicintgetAge(){returnage;}@IdpublicintgetId(){returnid;}publicStringgetName(){returnname;}publicvoidsetAge(intage){this.age=age;}publicvoidsetId(intid){this.id=id;}publicvoidsetName(Stringname){this.name=name;}}hibernate报错Unknowncolumn'tian1'in'fieldlist'小弟很急啊,弄了一天了,各位帮忙看看啊,小弟不胜感激
解决方案
解决方案二:
<propertyname="hbm2ddl.auto">update</property>-><propertyname="hbm2ddl.auto">create</property>
解决方案三:
该回复于2011-03-28 09:00:58被版主删除
解决方案四:
4个属性分别是create:每次启动时创建表结构,在建表前会先执行删除表的语句,以保证创建成功。update:每次启动时检测一下,如果表结构没有,则创建,不一样,则更新。create-drop:每次启动时创建表结构,退出前删除表结构validate:每次启动时检测一下,如果表结构不一样,就报错hbm2ddl.auto,一般在新增时有效,更新时无效。
解决方案五:
com/bjsxt/hibnate/model/student.hbm.xml这个配置文件内容呢
解决方案六:
该回复于2011-03-28 09:17:00被版主删除