hibernate创建表:hibernate annoation ( 2创建表)



  为了追踪hibernate信息 <property name="hibernate.show_sql">true</property>

  新建User类:

@Entity 
@Table(name="E_USER",uniqueConstras={ 
@UniqueConstra(columnNames={"yahoo"}) 
}) 
public  User { 
 
private  id; 
private String yahoo; //昵称唯 
 
@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
public  getId { 
 id; 
} 
public void Id( id) { 
this.id = id; 
} 
public String getYahoo { 
 yahoo; 
} 
public void Yahoo(String yahoo) { 
this.yahoo = yahoo; 
} 
 
}


  创建表 首先在hibernate.cfg.xml里配置<mapping ="com.eric.po.User"/>介绍说明:使用annoation同样可以接受.hbm.xml文件

  1以手动创建

  DROP TABLE IF EXISTS `e_user`; 
CREATE TABLE `e_user` ( 
 `id` (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>属性来自动创建

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

  create(true,true):两个参数:   

  Java代码   

* @param script pr the DDL to the console 
* @param export export the script to the database


  hibernate建表语句:

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


Tags:  hibernate生成表 hibernate多表查询 hibernate创建table hibernate创建表

延伸阅读

最新评论

发表评论