Hibernate自动生成表

Hibernate本意是冬眠,很好的封装了JDBC和数据库交互,实现了对象的持久化操作。所以也可以理解对象的持久化其实就是冬眠

自动创建|更新|验证数据库表结构

create
每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
create-drop :
每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
update
最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。

validate :

每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。

hibernate.cfg.xml中加一段配置:

<property name="hbm2ddl.auto">         </property> 

我们进行实例代码的编写:

1,新建web项目,导入所需的jar包,(导入的jar包一定要匹配)

2,新建实体类User.java

package entity;

public class User {
private int id;
private String name;
private String password;
public int getId() {
 return id;
}
public void setId(int id) {
 this.id = id;
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
public String getPassword() {
 return password;
}
public void setPassword(String password) {
 this.password = password;
}
public User() {
 super();
}

}
3,User.hbm.xml的编写

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="entity.User" table="user" catalog="mysong">
        <id name="id" type="java.lang.Integer">
           
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
           
        </property>
       
        <property name="password" type="java.lang.String">
           
        </property>
    </class>
</hibernate-mapping>
4,hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
     <property name="show_sql">true</property>
     
        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/mysong</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>
        
               <mapping resource="entity/User.hbm.xml" />
                  
              </session-factory>


</hibernate-configuration>

 

当我们把hibernate.hbm2ddl.auto=createhibernate先用hbm2ddl来生成数据库schema。当我们把hibernate.cfg.xml文件中hbm2ddl属性注释掉,这样我们就取消了在启动时用hbm2ddl来生成数据库schema。通常 只有在不断重复进行单元测试的时候才需要打开它,但再次运行hbm2ddl会把你保存的一切都删除掉(drop---- create配置的含义是:在创建SessionFactory的时候,从scemadrop掉所以的表,再重新创建它们hbm2ddl会在第一次运行的时候创建数据库schema, 后续的应用程序重启后还能继续使用这个schema。假若你修改了映射,或者修改了数据库schema,你必须把hbm2ddl重新打开一次。

猜你喜欢

转载自blog.csdn.net/aiwqsunny/article/details/80500443