<property name="hibernate.hbm2ddl.auto" value="create" />表或视图不存在是不是你把value设为update了?它以为你要做的动作是更新,所以就找不到表报错咯。
参考代码:================================================== 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新建的表。
81,122
社区成员
341,744
社区内容
加载中
试试用AI创作助手写篇文章吧