hibenate.hbm2ddl.auto怎么用啊?!!

951144125 Java开发工程师小菜鸟  2015-11-30 11:47:50
ssh框架写的东东,用的是oracle数据库,数据库里面没有建表。在applicationContext.xml中想通过hibenate.hbm2ddl.auto设置属性自动生成表,只是我每次部署完后,运行报错,说表或视图不存在。我在网上找了关于hibenate.hbm2ddl.auto属性的详解,应该是能自动生成表的,但是却没有生成,这是怎么回事呢?大神帮给我解答一下!!!
...全文
56 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
951144125 2015-12-02
引用 2 楼 sinat_31240689 的回复:
<property name="hibernate.hbm2ddl.auto" value="create" />表或视图不存在是不是你把value设为update了?它以为你要做的动作是更新,所以就找不到表报错咯。
这种创建表或视图,他是放在哪个表空间下面,默认的吗?我能通过代码来设置表空间吗?
回复
l337krew543 2015-12-02
错误信息有吗?
回复
l337krew543 2015-12-02
update的意思是如果数据库没有这张表,则生成这张表,否则直接对该表做操作
回复
951144125 2015-12-02
引用 3 楼 l337krew543 的回复:
参考代码:================================================== hibernate.hbm.xml <?xml version="1.0" encoding="GBK"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">orcl</property> <property name="connection.password">orcl</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- 根据需要自动创建数据库 --> <property name="hbm2ddl.auto">update</property> <!-- 显示Hibernate持久化操作所生成的SQL --> <property name="show_sql">true</property> <!-- 将SQL脚本进行格式化后再输出 --> <property name="hibernate.format_sql">true</property> <!-- 罗列所有的映射文件 --> <mapping resource="com/domain/UserInf.hbm.xml"/> </session-factory> </hibernate-configuration> 实体类============================== public class UserInf { private int userid; private String username; public UserInf(){} public UserInf(int userid, String username){ this.userid = userid; this.username = username; } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } UserInf.hbm.xml=========================== <?xml version="1.0" encoding="GBK"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.domain"> <class name="UserInf" table="XXOO"> <id name="userid" column="userid" type="int"> <generator class="sequence"> <param name="sequence">userid</param> </generator> </id> <property name="username" column="username" type="string"/> </class> </hibernate-mapping> hibernate驱动类 public class Test { public static SessionFactory sf; static{ try { Configuration cfg = new Configuration().configure(); sf = cfg.buildSessionFactory(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static final ThreadLocal<Session> session = new ThreadLocal<Session>(); public static Session currentSession() throws HibernateException { Session s = session.get(); if (s == null) { s = sf.openSession(); session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = session.get(); if (s != null) s.close(); session.set(null); } } main方法实现类================= public class TestHib { public static void main(String[] args) { TestHib t = new TestHib(); t.createAndStorePerson(); Test.sf.close(); } private void createAndStorePerson() { Session session = Test.currentSession(); Transaction tx = session.beginTransaction(); UserInf user = new UserInf(); user.setUsername("hello"); session.save(user); tx.commit(); Test.closeSession(); } } hibernate.hbm2ddl.auto:update、create、create-drop update:当hbm。xml里的table值不存在数据库则自动新建,否则正常使用 create:直接新建 create-drop:设置sessionFactory时,使用create-drop值时,显示关闭SessionFactory时,将Drop新建的表。
我今天又试了,还是没有自动生成表,不管是create,还是update
回复
<property name="hibernate.hbm2ddl.auto" value="create" /> [color=#FF0000]<!-- 根据需要自动创建数据库 --> <property name="hbm2ddl.auto">update</property>[/color] 就是这行代码 应该是默认的表空间,怎么用代码修改我就不知道了,创建表之后还要把这个改回update,不然启动一次就重新创建一次表,数据不保存
回复
l337krew543 2015-11-30
参考代码:================================================== hibernate.hbm.xml <?xml version="1.0" encoding="GBK"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">orcl</property> <property name="connection.password">orcl</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- 根据需要自动创建数据库 --> <property name="hbm2ddl.auto">update</property> <!-- 显示Hibernate持久化操作所生成的SQL --> <property name="show_sql">true</property> <!-- 将SQL脚本进行格式化后再输出 --> <property name="hibernate.format_sql">true</property> <!-- 罗列所有的映射文件 --> <mapping resource="com/domain/UserInf.hbm.xml"/> </session-factory> </hibernate-configuration> 实体类============================== public class UserInf { private int userid; private String username; public UserInf(){} public UserInf(int userid, String username){ this.userid = userid; this.username = username; } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } UserInf.hbm.xml=========================== <?xml version="1.0" encoding="GBK"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.domain"> <class name="UserInf" table="XXOO"> <id name="userid" column="userid" type="int"> <generator class="sequence"> <param name="sequence">userid</param> </generator> </id> <property name="username" column="username" type="string"/> </class> </hibernate-mapping> hibernate驱动类 public class Test { public static SessionFactory sf; static{ try { Configuration cfg = new Configuration().configure(); sf = cfg.buildSessionFactory(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static final ThreadLocal<Session> session = new ThreadLocal<Session>(); public static Session currentSession() throws HibernateException { Session s = session.get(); if (s == null) { s = sf.openSession(); session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = session.get(); if (s != null) s.close(); session.set(null); } } main方法实现类================= public class TestHib { public static void main(String[] args) { TestHib t = new TestHib(); t.createAndStorePerson(); Test.sf.close(); } private void createAndStorePerson() { Session session = Test.currentSession(); Transaction tx = session.beginTransaction(); UserInf user = new UserInf(); user.setUsername("hello"); session.save(user); tx.commit(); Test.closeSession(); } } hibernate.hbm2ddl.auto:update、create、create-drop update:当hbm。xml里的table值不存在数据库则自动新建,否则正常使用 create:直接新建 create-drop:设置sessionFactory时,使用create-drop值时,显示关闭SessionFactory时,将Drop新建的表。
回复
<property name="hibernate.hbm2ddl.auto" value="create" />表或视图不存在是不是你把value设为update了?它以为你要做的动作是更新,所以就找不到表报错咯。
回复
951144125 2015-11-30
没有大神来帮我解决一些的吗?!
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-11-30 11:47
社区公告
暂无公告