1.hibernate多对一单向关系映射(外键永远会加在多的一方)
package yingjun.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Person { private int id; private String name; private int age; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package yingjun.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Dream { private int dreamid; private String dream; private Person person; @Id @GeneratedValue public int getDreamid() { return dreamid; } public void setDreamid(int dreamid) { this.dreamid = dreamid; } public String getDream() { return dream; } public void setDream(String dream) { this.dream = dream; } public void setPerson(Person person) { this.person = person; } @ManyToOne //建立多对一联系 @JoinColumn(name="personid") public Person getPerson() { return person; } }
create table Dream ( dreamid integer not null auto_increment, dream varchar(255), personid integer, primary key (dreamid) ) create table Person ( id integer not null auto_increment, age integer not null, name varchar(255), primary key (id) ) alter table Dream add index FK3F397E3E07A4B5E (personid), add constraint FK3F397E3E07A4B5E foreign key (personid) references Person (id)
2.hibernate一对多单向关系映射(外键永远会加在多的一方)
package yingjun.model; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; @Entity public class Person { private int id; private String name; private int age; private Set<Dream> dream=new HashSet<Dream>(); @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @OneToMany @JoinColumn(name="personid")//要加这个字段 不然会用多对多的方式生产3张表 public Set<Dream> getDream() { return dream; } public void setDream(Set<Dream> dream) { this.dream = dream; } }
package yingjun.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Dream { private int dreamid; private String dream; @Id @GeneratedValue public int getDreamid() { return dreamid; } public void setDreamid(int dreamid) { this.dreamid = dreamid; } public String getDream() { return dream; } public void setDream(String dream) { this.dream = dream; } }
create table Dream ( dreamid integer not null auto_increment, dream varchar(255), personid integer, primary key (dreamid) ) create table Person ( id integer not null auto_increment, age integer not null, name varchar(255), primary key (id) ) alter table Dream add index FK3F397E3E07A4B5E (personid), add constraint FK3F397E3E07A4B5E foreign key (personid) references Person (id)
3.hibernate多对一(一对多)双向关系映射
package yingjun.model; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.MappedSuperclass; import javax.persistence.OneToMany; @Entity public class Person { private int id; private String name; private int age; private Set<Dream> dream=new HashSet<Dream>(); @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @OneToMany(mappedBy="person") //mappedBy要加在One这方 public Set<Dream> getDream() { return dream; } public void setDream(Set<Dream> dream) { this.dream = dream; } }
package yingjun.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Dream { private int dreamid; private String dream; private Person person; @Id @GeneratedValue public int getDreamid() { return dreamid; } public void setDreamid(int dreamid) { this.dreamid = dreamid; } public String getDream() { return dream; } public void setDream(String dream) { this.dream = dream; } @ManyToOne public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
create table Dream ( dreamid integer not null auto_increment, dream varchar(255), person_id integer, primary key (dreamid) ) create table Person ( id integer not null auto_increment, age integer not null, name varchar(255), primary key (id) ) alter table Dream add index FK3F397E3F8EABE73 (person_id), add constraint FK3F397E3F8EABE73 foreign key (person_id) references Person (id)