瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          hibernate annoation (二創建表)

          Posted on 2009-11-02 14:58 瘋狂 閱讀(886) 評論(0)  編輯  收藏 所屬分類: java hibernate

          為了追蹤hibernate的信息 <property name="hibernate.show_sql">true</property>

           新建User類:

           

          @Entity
          @Table(name="E_USER",uniqueConstraints={
          @UniqueConstraint(columnNames={"yahoo"})
          })
          public class User {

          private int id;
          private String yahoo; //昵稱唯一

          @Id
          @GeneratedValue(strategy=GenerationType.AUTO)
          public int getId() {
          return id;
          }
          public void setId(int id) {
          this.id = id;
          }
          public String getYahoo() {
          return yahoo;
          }
          public void setYahoo(String yahoo) {
          this.yahoo = yahoo;
          }

          }

           創建表 首先在hibernate.cfg.xml里配置<mapping class="com.eric.po.User"/>說明:使用annoation同樣可以接受.hbm.xml文件

           1,以手動創建

             DROP TABLE IF EXISTS `e_user`;
          CREATE TABLE `e_user` (
            `id` int(11) NOT NULL auto_increment,
            `yahoo` varchar(255) default NULL,
            PRIMARY KEY  (`id`),
            UNIQUE KEY `yahoo` (`yahoo`)
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

           2,使用<property name="hbm2ddl.auto">create</property>屬性來自動創建

           3,SchemaExport : new SchemaExport(new AnnotationConfiguration().configure()).create(true,true);

               create(true,true):兩個參數:   

          Java代碼 復制代碼
          1. @param script print the DDL to the console   
          2. @param export export the script to the database  

             hibernate建表語句:

            drop table if exists E_USER
           create table E_USER (id integer not null auto_increment, yahoo varchar(255), primary key (id), unique (yahoo))

          主站蜘蛛池模板: 金华市| 抚远县| 贡觉县| 奉新县| 和龙市| 剑阁县| 托里县| 寿光市| 台山市| 中超| 金湖县| 肥西县| 聂拉木县| 万荣县| 凤城市| 大田县| 三门峡市| 清丰县| 孟津县| 平遥县| 星座| 岚皋县| 来凤县| 莱芜市| 霸州市| 嘉黎县| 南平市| 淳化县| 玉溪市| 固阳县| 西林县| 涿州市| 安陆市| 黔南| 扎囊县| 靖边县| 扶风县| 滁州市| 武强县| 兴化市| 高青县|