hibernate annoation (二创建表)
2009-09-18 00:00:00 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鎯у⒔閹虫捇鈥旈崘顏佸亾閿濆簼绨绘い鎺嬪灪閵囧嫰骞囬姣挎捇鏌熸笟鍨妞ゎ偅绮撳畷鍗炍旈埀顒勭嵁婵犲嫮纾介柛灞捐壘閳ь剛鎳撻~婵嬪Ω閳轰胶鐤呯紓浣割儐椤戞瑩宕ョ€n喗鐓曟い鎰靛亝缁舵氨绱撻崘鈺傜婵﹤顭峰畷鎺戔枎閹搭厽袦婵犵數濮崑鎾绘⒑椤掆偓缁夌敻骞嗛悙鍝勭婵烇綆鍓欐俊鑲╃磼閹邦収娈滈柡灞糕偓鎰佸悑閹肩补鈧尙鏁栧┑鐐村灦閹稿摜绮旈悽绋课﹂柛鏇ㄥ灠閸愨偓濡炪倖鍔﹀鈧繛宀婁邯濮婅櫣绱掑Ο璇茶敿闂佺ǹ娴烽弫璇差嚕婵犳碍鏅插璺猴工瀹撳棝姊虹紒妯哄缂佷焦鎸冲畷鎴﹀箻鐠囧弶宓嶅銈嗘尰缁嬫垶绂嶉悙顒佸弿婵☆垳鍘ф禍楣冩倵濮樼偓瀚�

为了追踪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代码
* @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))
- ››创建SQL2005自动备份,定期删除的维护计划
- ››Hibernate高级应用:性能优化策略
- ››hibernate 多对多关系详解(包括中间表,一对多字表...
- ››Hibernate实现mysql数据库limit查询方法
- ››创建动态表单 javascript
- ››创建基于PPTP的站点到站点VPN连接:ISA2006系列之...
- ››创建基于L2TP的站点到站点的VPN连接:ISA2006系列...
- ››创建一个Twisted Reactor TCP服务器
- ››创建Windows Mobile上兼容性好的UI 程序
- ››创建android的Service
- ››创建远古部落环境与原住民角色
- ››创建并扩展Apache Wicket Web应用
更多精彩
赞助商链接