标签:数据库 hibernate manytoone onetomany 关联
本文可作为北京尚学堂马士兵hibernate课程的学习笔记。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
标签:数据库 hibernate manytoone onetomany 关联
原文地址:http://blog.csdn.net/dlf123321/article/details/46432119