hibernate annoation (八 关联映射)
2009-09-18 00:00:00 来源:WEB开发网如果没有写@JoinColumn(name="b")则默认是关联属性名+下划线+id
最终sql:
Java代码
create table A (id integer not null auto_increment, aname varchar(255), b_id integer, primary key (id))
create table B (id integer not null auto_increment, bname varchar(255), primary key (id))
alter table A add index FK41FCD34905 (b_id), add constraint FK41FCD34905 foreign key (b_id) references B (id)
可以使用@JoinColumn(referencedColumnName="bname")让主关联方不关联被关联放的主键
最终sql
Java代码
create table A (id integer not null auto_increment, aname varchar(255), b_bname varchar(255), primary key (id))
create table B (id integer not null auto_increment, bname varchar(255), primary key (id), unique (bname))
alter table A add index FK41E47CD6BD (b_bname), add constraint FK41E47CD6BD foreign key (b_bname) references B (bname)
3 关联表
使用
@OneToOne(cascade=CascadeType.ALL)
@JoinTable(name="centert",joinColumns=@JoinColumn(name="aid"),inverseJoinColumns=@JoinColumn(name="bid"))
最终sql:
写道
create table A (id integer not null auto_increment, aname varchar(255), primary key (id))
create table B (id integer not null auto_increment, bname varchar(255), primary key (id))
create table centert (bid integer, aid integer not null, primary key (aid))
alter table centert add index FK27A6BEBFFCA6C7EA (bid), add constraint FK27A6BEBFFCA6C7EA foreign key (bid) references B (id)
alter table centert add index FK27A6BEBFFCA6C428 (aid), add constraint FK27A6BEBFFCA6C428 foreign key (aid) references A (id)
- ››Hibernate高级应用:性能优化策略
- ››hibernate 多对多关系详解(包括中间表,一对多字表...
- ››Hibernate实现mysql数据库limit查询方法
- ››Hibernate 之父:是时候升级到Java EE 6了
- ››Hibernate查询
- ››Hibernate和iBATIS比较(摘自网络)
- ››Hibernate使用Projections进行聚合操作
- ››hibernate中java.util.Date类型映射
- ››hibernate中update与saveOrUpdate的区别
- ››Hibernate各种映射关系总结
- ››Hibernate过滤器使用窍门
- ››Hibernate属性查询简单讲述
更多精彩
赞助商链接