日期:2014-05-19  浏览次数:20739 次

Hibernate启动后不能自动建表
有两个实体Order和User,user和order的关系为一对多关系。但是我启动项目时不能自动建表。数据库我已经建立。
代码如下:

User.java
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private int id;
private String username;
private String password;
private String sex;
private Date birthday;
private String email;
private int type;
private Set<Order> orders;
}
Order.java
public class Order implements java.io.Serializable {

private static final long serialVersionUID = 1L;
private int id;
/*
 * 订单编号
 */
private String orderCode;
private User user;
}
Order.hbm.xml
<hibernate-mapping package="entity">
<class name="Order" table="order">
<id name="id" column="id" type="integer">
<generator class="native"></generator>
</id>
<property name="orderCode" type="string">
<column name="order_code"></column>
</property>

</class>
</hibernate-mapping>
User.hbm.xml
<hibernate-mapping package="entity">
<class name="User" table="user">
<id name="id" column="id" type="integer">
<generator class="native"></generator>
</id>
<property name="username" type="string">
<column name="username"></column>
</property>
<property name="password" type="string">
<column name="password"></column>
</property>
<property name="sex" type="string">
<column name="sex"></column>
</property>
<property name="birthday" type="timestamp">
<column name="birthday"></column>
</property>
<property name="email" type="string">
<column name="email"></column>
</property>
<property name="type" type="integer">
<column name="type"></column>
</property>
<set name="orders" inverse="true" cascade="all">
<key column="user_id"></key>
<one-to-many class="Order"/>
</set>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.url" >jdbc:mysql://localhost:3306/reg</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.hbm2ddl.auto">all</property>
        <property name="current_session_co