hibernate annoation (二创建表)

为了追踪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):两个参数:   

* @param script print the DDL to the console
* @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))

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值