开发环境jdk1.6+tomcat6+myeclipse8GA+mysql5.1
原型类User
package www.gh.usermodule;public class User {

private String userName;
private String password;
private String id;
public String getUserName() {return userName; }
public void setUserName(String userName) {this.userName = userName;}
public String getPassword() {return password; }
public void setPassword(String password) {this.password = password;}
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public User(String userName, String password) {this.userName = userName; this.password = password;}
}生成hibernate映射xml文件为User.hbm.hibernate,同样放在package www.gh.usermodule包下,配置如下
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="www.gh.usermodule">
 <class name="User" table="user">
  <id column="id" name="id">
   <generator class="identity"/>
  </id>
  <property column="userName" generated="never" lazy="false" name="userName"/>
  <property column="password" generated="never" lazy="false" name="password"/>
 </class>
</hibernate-mapping>
hibernate配置文件内容如下
<?xml version='1.0' encoding='UTF-8'?>
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver1
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/GH
</property>
<property name="connection.username">root</property>
<property name="connection.password">123321</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="www/gh/usermodule/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
由于是利用Myeclipse自动添加hibernate功能的,自动生成了HibernateSessionFactory类,
写了个测试类UserTest内容如下package www.gh.usermodule;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;import www.gh.hibernate.HibernateSessionFactory;
public class UserTest { public static void main(String[] args) {

SessionFactory sf;
Session ss;
Transaction t;
try {
sf = HibernateSessionFactory.getSessionFactory();
ss = sf.openSession();

t = ss.beginTransaction();
//--------------------创建表-----------------------
//SchemaExport se = new SchemaExport(HibernateSessionFactory.getConfiguration());
 
//se.create(true, true);
//System.out.println("Table created...");

ss.save(new User("user1","password1"));
t.commit();
//ss.save(new User("user1","password1"));
//t.commit();
ss.close();
sf.close();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}}将这三行
//SchemaExport se = new SchemaExport(HibernateSessionFactory.getConfiguration());
//se.create(true, true);
//System.out.println("Table created...");注释去掉,将ss.save(new User("user1","password1"));注释掉,运行得到console输出结果为
drop table if exists user
create table user (id varchar(255) not null auto_increment, userName varchar(255), password varchar(255), primary key (id))
Table created...
反过来再注释掉三行,去掉一行注释,则输出结果为
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'gh.user' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2398)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2316)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2301)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 16 more
不知道原因是什么?高手指教了!