1、一对一单向外键关联
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8</property> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="hbm2ddl.auto">update</property> <!-- SQL dialect --> <property name="dialect"> org.hibernate.dialect.MySQL5InnoDBDialect </property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping class="com.pt.hibernate.Husband" /> <mapping class="com.pt.hibernate.Wife" /> </session-factory> </hibernate-configuration>
hibernate.cfg.xml
package com.pt.hibernate; import java.util.Date; import javax.annotation.Generated; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; @Entity public class Husband { int id; String name; Date bornTime; Wife myWife ; @OneToOne @JoinColumn(name="wifiId") //指定外键字段名 public Wife getMyWife() { return myWife; } public void setMyWife(Wife myWife) { this.myWife = myWife; } @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 Date getBornTime() { return bornTime; } public void setBornTime(Date bornTime) { this.bornTime = bornTime; } }
Husband.java
package com.pt.hibernate; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Wife { int id; @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 Date getBornTime() { return bornTime; } public void setBornTime(Date bornTime) { this.bornTime = bornTime; } String name; Date bornTime; }
Wife
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Test { public static void main(String[] arges){ Configuration cfg = new Configuration(); SessionFactory factory = cfg.configure().buildSessionFactory(); } }
Test
2、一对一双向外键关联
两个相关联的实体类都写上@OneToOne即可,如果指定OneToOne的(mappedBy="")属性,这样就不会在两个表中都保留对方的ID,只会在其中一个表中保留对方的ID。只要有双向关联,该属性必设置。
package com.pt.hibernate; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Wife { int id; String name; Date bornTime; Husband myHusband ; @OneToOne(mappedBy="myWife") public Husband getMyHusband() { return myHusband; } public void setMyHusband(Husband myHusband) { this.myHusband = myHusband; } @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 Date getBornTime() { return bornTime; } public void setBornTime(Date bornTime) { this.bornTime = bornTime; } }
Wife
3、多对一单向外键关联
多对一,数据库中的表现是在多的一方记录一的一方的ID,添加Son类,同时在配置文件中配置,Son类如下:
package com.pt.hibernate; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Son { int id; String name; Husband myFather; @ManyToOne @JoinColumn(name="fatherId") public Husband getMyFather() { return myFather; } public void setMyFather(Husband myFather) { this.myFather = myFather; } @Id 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; } }
Son
4、多对一双向外键关联
在3、的基础上,修改Husband类,如下:
package com.pt.hibernate; import java.util.Date; import java.util.Set; import javax.annotation.Generated; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.OneToOne; @Entity public class Husband { int id; String name; Date bornTime; Wife myWife ; Set<Son> sons; @OneToMany(mappedBy="myFather") public Set<Son> getSons() { return sons; } public void setSons(Set<Son> sons) { this.sons = sons; } @OneToOne @JoinColumn(name="wifiId") //指定外键字段名 public Wife getMyWife() { return myWife; } public void setMyWife(Wife myWife) { this.myWife = myWife; } @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 Date getBornTime() { return bornTime; } public void setBornTime(Date bornTime) { this.bornTime = bornTime; } }
Husband
时间: 2024-10-12 09:30:07