Hibernate之JPA的关联映射

JPA的关联映射:

  1.导包,位于hibernate-lib-jpa里面的包;

   删除对Java EE 5 Libraries的使用。

  2.书写实体类,书写注释。注意注释是导的javax.类;

  @id

  @GeneratedValue 为主键生成策略(默认为native);

View Code
 1 import javax.persistence.Entity;

 2 import javax.persistence.GeneratedValue;

 3 import javax.persistence.Id;

 4 import javax.persistence.Table;

 5 

 6 

 7 @Entity

 8 @Table(name="a_user")

 9 public class User {

10     private int id;

11     private String name;

12     private int age;

13 

14     @Id

15     @GeneratedValue

16     public int getId() {

17         return id;

18     }

19     public void setId(int id) {

20         this.id = id;

21     }

  3.hibernate.cfg.xml中配置实体类,<mapping class="com.kebin.pojo.User" />

hibernate.cfg.xml
 1 <!DOCTYPE hibernate-configuration PUBLIC

 2     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

 3     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 4 

 5 <hibernate-configuration>

 6 <session-factory name="foo">

 7     <property name="show_sql">true</property>

 8     <property name="myeclipse.connection.profile">MYSQL</property>

 9     <property name="connection.url">

10         jdbc:mysql://127.0.0.1:3306/mysql

11     </property>

12     <property name="connection.username">root</property>

13     <property name="connection.password">root</property>

14     <property name="connection.driver_class">

15         com.mysql.jdbc.Driver

16     </property>

17     <property name="dialect">

18         org.hibernate.dialect.MySQL5Dialect

19     </property>

20     <property name="format_sql">true</property>

21     <property name="hbm2ddl.auto">update</property>

22     <mapping class="com.kebin.pojo.User" />

23 

24 </session-factory>

25 </hibernate-configuration>

  4.书写方法,创建表格;简单的配置就完成了,直接运行。

Userutil
 1 package com.kebin.util;

 2 

 3 import org.hibernate.cfg.AnnotationConfiguration;

 4 import org.hibernate.cfg.Configuration;

 5 import org.hibernate.tool.hbm2ddl.SchemaExport;

 6 

 7 public class Userutil {

 8 

 9     public static void main(String[] args) {

10         Configuration cfg = new AnnotationConfiguration().configure();

11         SchemaExport se = new SchemaExport(cfg);

12         se.create(true, true);

13 

14     }

15 

16 }

一、多对一关联映射

  项目一般都用双向配置,所以以下只给出双向的。

  1.书写两个类,然后再hibernate.cfg.xml中配置实体类。

  2. 多的一端维护:@ManyToOne;        添加一个外键。
                        @JoinColumn(name="cid") ;     cid根据注释代码get方法的类取名。

  3.少的一端:@OneToMany(mappedBy="classes");

   mappedBy方法只管加载数据,外键关系交给""处理。

  4.hibernate.cfg.xml中配置: <mapping class="com.kebin.pojo.Class" />
                  <mapping class="com.kebin.pojo.Student" />

 

Student.java
 1 @Entity

 2 @Table(name="t_student")

 3 public class Student {

 4     private int id;

 5     private String name;

 6     private Class classes;

 7 

 8 

 9     @Id

10     @GeneratedValue

11     public int getId() {

12         return id;

13     }

14     public void setId(int id) {

15         this.id = id;

16     }

17     public String getName() {

18         return name;

19     }

20     public void setName(String name) {

21         this.name = name;

22     }

23     @ManyToOne

24     @JoinColumn(name="cid")

25     public Class getClasses() {

26         return classes;

27     }

28     public void setClasses(Class classes) {

29         this.classes = classes;

30     }

 

Class.java
 1 @Entity

 2 @Table(name="t_class")

 3 public class Class {

 4     private int id;

 5     private String name;

 6     private Set<Student> students = new HashSet<Student>();

 7     

 8     @Id

 9     @GeneratedValue

10     public int getId() {

11         return id;

12     }

13     public void setId(int id) {

14         this.id = id;

15     }

16     public String getName() {

17         return name;

18     }

19     public void setName(String name) {

20         this.name = name;

21     }

22     @OneToMany(mappedBy="classes")

23     public Set<Student> getStudents() {

24         return students;

25     }

26     public void setStudents(Set<Student> students) {

27         this.students = students;

28     }

 一、一对一关联映射

   用得比较少,一般直接把多对一关系的外键设置唯一属性即可;

   1.一端:@ManyToOne;   

           @JoinColumn(name="cid",unique=true) ;

  2.另一端:@OneToOne(mappedBy="wife");代码如下

Husband.java
 1 @Entity

 2 @Table(name="t_husband")

 3 public class Husband {

 4     private int id;

 5     private String name;

 6     private Wife wife;

 7     @Id

 8     @GeneratedValue

 9     public int getId() {

10         return id;

11     }

12     public void setId(int id) {

13         this.id = id;

14     }

15     public String getName() {

16         return name;

17     }

18     public void setName(String name) {

19         this.name = name;

20     }

21     @ManyToOne

22     @JoinColumn(name="wid",unique=true)

23     public Wife getWife() {

24         return wife;

25     }

26     public void setWife(Wife wife) {

27         this.wife = wife;

28     }

29     
Wife.java
 1 @Entity

 2 @Table(name="t_wife")

 3 public class Wife {

 4     private int id;

 5     private String name;

 6     private Husband husband;

 7     

 8     

 9     @Id

10     @GeneratedValue

11     public int getId() {

12         return id;

13     }

14     public void setId(int id) {

15         this.id = id;

16     }

17     public String getName() {

18         return name;

19     }

20     public void setName(String name) {

21         this.name = name;

22     }

23     @OneToOne(mappedBy="wife")

24     public Husband getHusband() {

25         return husband;

26     }

27     public void setHusband(Husband husband) {

28         this.husband = husband;

29     }

 一、一对多关联映射

   这个就不介绍了,和多对一差不多。

 一、多对多关联映射

   1.一端:@ManyToMany

       @JoinTable(name="t_teacher_class",joinColumns={@JoinColumn(name="tid")}, inverseJoinColumns={@JoinColumn(name="cid")})

  2.另一端:@ManyToMany(mappedBy="classes")

Teacher.java
 1 @Entity

 2 @Table(name="t_teacher")

 3 public class Teacher {

 4     private int id;

 5     private String name;

 6     private Set<Class> classes = new HashSet<Class>();

 7     

 8     @Id

 9     @GeneratedValue

10     public int getId() {

11         return id;

12     }

13     public void setId(int id) {

14         this.id = id;

15     }

16     public String getName() {

17         return name;

18     }

19     public void setName(String name) {

20         this.name = name;

21     }

22     @ManyToMany

23     @JoinTable(name="t_teacher_class",joinColumns={@JoinColumn(name="tid")},

24                 inverseJoinColumns={@JoinColumn(name="cid")})

25     public Set<Class> getClasses() {

26         return classes;

27     }

28     public void setClasses(Set<Class> classes) {

29         this.classes = classes;

30     }
Class.java
 1 @Entity

 2 @Table(name="t_class")

 3 public class Class {

 4     private int id;

 5     private String name;

 6     private Set<Teacher> teachers = new HashSet<Teacher>();

 7     

 8     @Id

 9     @GeneratedValue

10     public int getId() {

11         return id;

12     }

13     public void setId(int id) {

14         this.id = id;

15     }

16     public String getName() {

17         return name;

18     }

19     public void setName(String name) {

20         this.name = name;

21     }

22 

23     @ManyToMany(mappedBy="classes")

24     public Set<Teacher> getTeachers() {

25         return teachers;

26     }

27     public void setTeachers(Set<Teacher> teachers) {

28         this.teachers = teachers;

29     }

30     

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Hibernate)