WEB开发网
开发学院软件开发Java hibernate annoation (八 关联映射) 阅读

hibernate annoation (八 关联映射)

 2009-09-18 00:00:00 来源:WEB开发网   
核心提示: manytoone和onetoone很相似特殊情况:果不写:mappedBy这会产生中间表:Java代码createtableA(idintegernotnullauto_increment,anamevarchar(255),i_idinteger,primarykey(id))createt

manytoone

和onetoone很相似

特殊情况:果不写:mappedBy这会产生中间表:

Java代码  

create table A (id integer not null auto_increment, aname varchar(255), i_id integer, primary key (id)) 
create table B (id integer not null auto_increment, bname varchar(255), primary key (id)) 
create table B_A (B_id integer not null, a_id integer not null, unique (a_id)) 
alter table A add index FK41FCD6779E (i_id), add constraint FK41FCD6779E foreign key (i_id) references B (id) 
alter table B_A add index FK10384FCD34905 (B_id), add constraint FK10384FCD34905 foreign key (B_id) references B (id) 
alter table B_A add index FK10384FCD2D4A5 (a_id), add constraint FK10384FCD2D4A5 foreign key (a_id) references A (id)

targetEntity属性可以关联接口

例如接口代码

Java代码   

public interface I { 
 
}

class B implments I

关联方:

Java代码   

private I i; 
@ManyToOne(targetEntity=B.class) 
 public I getI() { 
 return i; 
 }

最终sql:

Java代码

create table A (id integer not null auto_increment, aname varchar(255), i_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 FK41FCD6779E (i_id), add constraint FK41FCD6779E foreign key (i_id) references B (id)

上一页  1 2 3 

Tags:hibernate annoation 关联

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接