瘋狂

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

          hibernate annoation (二創(chuàng)建表)

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

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

           新建User類(lèi):

           

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

          private int id;
          private String yahoo; //昵稱(chēng)唯一

          @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;
          }

          }

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

           1,以手動(dòng)創(chuàng)建

             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>屬性來(lái)自動(dòng)創(chuàng)建

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

               create(true,true):兩個(gè)參數(shù):   

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

             hibernate建表語(yǔ)句:

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

          主站蜘蛛池模板: 思茅市| 东乡县| 佛冈县| 枞阳县| 绥宁县| 东光县| 龙里县| 南靖县| 五峰| 旌德县| 繁昌县| 开封市| 黄冈市| 紫金县| 盐源县| 安陆市| 饶平县| 永兴县| 余干县| 辽宁省| 定襄县| 镇巴县| 上高县| 和平区| 三门峡市| 高青县| 克拉玛依市| 浪卡子县| 古田县| 环江| 濉溪县| 丰顺县| 皮山县| 巢湖市| 隆安县| 河东区| 承德市| 石台县| 绥芬河市| 威远县| 灌阳县|