hibernate自動建表

2021-08-20 20:56:21 字數 1217 閱讀 8104

hibernate自動建立表的優缺點:

一、優點:

1、自動建立新錶

2、自動建立新字段

3、自動修改字段型別

二、缺點:

1、不會自動刪除表

2、不會自動刪除字段

3、自動建立的新字段只能是在最後。

hibernate支援自動建表,在開發階段很方便,可以保證hbm與資料庫表結構的自動同步。

一、通過hibernate的shemaexport來建立

1)實體類

package com.xiaomo.vo;

public class user

public user()

public user(string name, int age)

public int getid()

public void setid(int id)

public string getname()

public void setname(string name)

public int getage()

public void setage(int age)

}2)、user.hbm.xml檔案:

3)、hibernate.cfg.xml檔案:

"-//hibernate/hibernate configuration dtd 3.0//en"

"">

com.mysql.jdbc.driver

jdbc:mysql://localhost:3306/lili

root

cd_hisome

org.hibernate.dialect.mysqldialect  

update

true

4)、測試類:

package com.xiaomo.test;

import org.hibernate.cfg.configuration;

import org.hibernate.tool.hbm2ddl.schemaexport;

public class createtable

}結果:

drop table if exists user

create table user (id integer not null auto_increment, name varchar(255), age integer, primary key (id))

Hibernate自動建表

hibernate支援自動建表功能,需在hibernate.xml檔案中新增以下屬性 update 1 update 自動根據模型物件來更新表結構,啟動web應用程式時會自動檢查資料庫,並保證資料庫與模型物件關係一致。2 create 啟動web應用程式時,自動刪除原來的表,新建所有的表。3 cre...

hibernate自動建表之engine設定

1.mysql的資料庫引擎中,只有innodb和bdb berkley db 包括了對事務處理和外來鍵的支援。如果資料引擎建為myisam則rollback無效。2.而hibernate自動建表的時候語句如下 hibernate create table user id integer not nu...

Hibernate學習 三 自動建表

一般情況下有如下兩種方法 1 在配置檔案中新增如下配置 create 然後我們再執行任何檢索 更新等操作的時候就會自動建表 2.編寫乙個方法,方法內容如下 configuration conf new configuration conf.configure hibernate.cfg.xml sc...