package com.bjsxt.hibernate; @Entity public class Husband { private int id; private String name; private Wife wife; @Id @GeneratedValue public int getId() { return id; } @OneToOne @JoinColumn(name="wifeId") public Wife getWife() { return wife; } //省略get set } package com.bjsxt.hibernate; @Entity public class Wife { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } //省略get set }
package com.bjsxt.hibernate; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class Test { public static void main(String[] args) { new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); } }在hibernate的配置文件裡
<property name="hbm2ddl.auto">update</property>結果
19:41:04,229 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Husband ( id integer not null auto_increment, name varchar(255), wifeId integer, primary key (id) ) 19:41:04,549 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Wife ( id integer not null auto_increment, name varchar(255), primary key (id) ) 19:41:04,880 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Husband add index FKAEEA401B109E78ED (wifeId), add constraint FKAEEA401B109E78ED foreign key (wifeId) references Wife (id)
package com.bjsxt.hibernate; @Entity public class Wife { private int id; private String name; private Husband husband; @Id @GeneratedValue public int getId() { return id; } @OneToOne @JoinColumn(name="husbandId") public Husband getHusband() { return husband; } }
create table Husband ( id integer not null auto_increment, name varchar(255), wifeId integer, primary key (id) ) 19:53:20,487 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Wife ( id integer not null auto_increment, name varchar(255), husbandId integer, primary key (id) ) 19:53:20,824 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Husband add index FKAEEA401B109E78ED (wifeId), add constraint FKAEEA401B109E78ED foreign key (wifeId) references Wife (id) 19:53:21,421 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Wife add index FK292331CE01A6E1 (husbandId), add constraint FK292331CE01A6E1 foreign key (husbandId) references Husband (id)看見了吧,wife裡面有外鍵,husband裡面也有外鍵!
@OneToOne(mappedBy="wife") public Husband getHusband() { return husband; }這個mappedBy的意思是說,我(Wife這個類)和husband這個類是一對一關聯的,但是關聯的外鍵由husband類的getWife方法控制。
20:03:18,611 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Husband ( id integer not null auto_increment, name varchar(255), wifeId integer, primary key (id) ) 20:03:18,618 ERROR org.hibernate.tool.hbm2ddl.SchemaExport:348 - Unsuccessful: create table Husband (id integer not null auto_increment, name varchar(255), wifeId integer, primary key (id)) 20:03:18,618 ERROR org.hibernate.tool.hbm2ddl.SchemaExport:349 - Table 'husband' already exists 20:03:18,619 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Wife ( id integer not null auto_increment, name varchar(255), primary key (id) ) 20:03:18,933 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Husband add index FKAEEA401B109E78ED (wifeId), add constraint FKAEEA401B109E78ED foreign key (wifeId) references Wife (id)數據庫裡,wife表裡沒有外鍵了。
package com.bjsxt.hibernate; @Entity public class Dream { private int id; private String description; private Person person; @Id @GeneratedValue public int getId() { return id; } @ManyToOne @JoinColumn(name="personId") public Person getPerson() { return person; } //省略get set } package com.bjsxt.hibernate; @Entity public class Person { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } //省略get set }從代碼層次上看"多對一單向"
20:20:21,970 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Dream ( id integer not null auto_increment, description varchar(255), personId integer, primary key (id) ) 20:20:22,264 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Person ( id integer not null auto_increment, name varchar(255), primary key (id) ) 20:20:22,765 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table Dream add index FK3F397E3C1409475 (personId), add constraint FK3F397E3C1409475 foreign key (personId) references Person (id)
package com.bjsxt.hibernate; @Entity public class Person { private int id; private String name; private Set<Dream> dreams=new HashSet<>(); @Id @GeneratedValue public int getId() { return id; } @OneToMany @JoinColumn(name="psrsonId") public Set<Dream> getDreams() { return dreams; } }
package com.bjsxt.hibernate; @Entity public class Dream { private int id; private String description; private Person person; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } @ManyToOne @JoinColumn(name="personId") public Person getPerson() { return person; } } package com.bjsxt.hibernate; @Entity public class Person { private int id; private String name; private Set<Dream> dreams=new HashSet<>(); @Id @GeneratedValue public int getId() { return id; } @OneToMany(mappedBy="person") public Set<Dream> getDreams() { return dreams; } }ok,我們可以在代碼裡,通過dream獲得person也可以通過person獲得dream
package com.bjsxt.hibernate; @Entity public class Student { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } } OK,學生裡面沒有老師的引用。 package com.bjsxt.hibernate; @Entity public class Teacher { private int id; private String name; private Set<Student> students = new HashSet<Student>(); @Id @GeneratedValue public int getId() { return id; } @ManyToMany @JoinTable(name="t_s", joinColumns={@JoinColumn(name="teacher_id")}, inverseJoinColumns={@JoinColumn(name="student_id")} ) public Set<Student> getStudents() { return students; } }看結果:
21:10:35,854 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Student ( id integer not null auto_increment, name varchar(255), primary key (id) ) 21:10:36,192 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table Teacher ( id integer not null auto_increment, name varchar(255), primary key (id) ) 21:10:36,643 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - create table t_s ( teacher_id integer not null, student_id integer not null, primary key (teacher_id, student_id) ) 21:10:36,947 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table t_s add index FK1BF68BF77BA8A (teacher_id), add constraint FK1BF68BF77BA8A foreign key (teacher_id) references Teacher (id) 21:10:37,588 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 - alter table t_s add index FK1BF68AEDC6FEA (student_id), add constraint FK1BF68AEDC6FEA foreign key (student_id) references Student (id) 21:10:38,263 INFO org.hibernate.tool.hbm2ddl.SchemaExport:268 - schema export complete
在上面的例子裡
改變student,給它加上teacher的引用。
package com.bjsxt.hibernate; @Entity public class Student { private int id; private String name; private Set<Teacher> teachers=new HashSet<>(); @Id @GeneratedValue public int getId() { return id; } //記著 雙向的時候 就加上mappedBy @ManyToMany(mappedBy="students") public Set<Teacher> getTeachers() { return teachers; } }
glt likes dlf
dlf likes glt
oneToOne