JPA 映射关系

单向关联

@OneToMany

一对多的单向关联,只需在代表一的实体(Author)中使用@OneToMany映射标注就可以了,代表多的实体不需要使用任何映射标注。有两种方式实现一对多的单向关联。

a. targetEntity属性表示默认关联的实体类型。如果集合类中指定了具体类型了,不需要使用targetEntity.否则要指定targetEntity=AddressEO.class。 

b. mappedBy属性用于标记当实体之间是双向时使用。

  1. 映射策略—表关联。只使用@OneToMany来标识,这种方式是通过一张第三方表来保存关系。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    
    @Entity
    @Table(name="author")
    public class Author {
       @Id
       @GeneratedValue
       private Long id;
       
       /**作者的名字*/
       @Column(length=32)
       private String name;
           
       /**作者写的书*/
         @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)//级联保存、更新、删除、刷新;延迟加载
       private Set<Book> books = new HashSet<Book>();
       
    @Entity
    @Table(name="book")
    public class Book {
           
       @Id
       @GeneratedValue
       private Long id;
           
       /**书名*/
       @Column(length=32)
       private String name;
    }

默认注解为@JoinTable:

1
2
3
   @OneToMany(cascade = {CascadeType.ALL})
   @JoinTable(name = "author_book", joinColumns = {@JoinColumn(name = "author_id", referencedColumnName = "id")},inverseJoinColumns = {@JoinColumn(name = "books_id", referencedColumnName = "id")})
   private Set<Book> books = new HashSet<Book>();
1
2
3
4
5
6
   mysql> show tablse;
   |Tables_in_jpa|
   |-------------|
   |author       |
   |author_book  |
   |book         |
1
2
3
4
5
   mysql> desc author_book;
   |Field     |type  |...|
   |----------|------|---|
   |author_id |      |   |
   |books_id  |      |   |
  1. 映射策略—外键关联。使用@OneToMany和@JoinColumn来标注,这种方式是在多的一方(Book)的表中增加一个外键列来保存关系。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    
    @Entity
    @Table(name="author")
    public class Author {
    @Id
    @GeneratedValue
    private Long id;
    
    /**作者的名字*/
    @Column(length=32)
    private String name;
        
    @OneToMany(	cascade=CascadeType.ALL,fetch=FetchType.LAZY)//级联保存、更新、删除、刷新;延迟加载
    @JoinColumn(name="author_id")//在book表增加一个外键列来实现一对多的单向关联
    private Set<Book> books = new HashSet<Book>();
    
    @Entity
    @Table(name="book")
    public class Book {
        
    @Id
    @GeneratedValue
    private Long id;
        
    /**书名*/
    @Column(length=32)
    private String name;
    }
    1
    2
    3
    4
    5
    
    mysql> show tablse;
    |Tables_in_jpa|
    |-------------|
    |author       | |
    |book         |
    1
    2
    3
    4
    5
    6
    
    mysql> desc book;
    |Field     |type  |...|
    |----------|------|---|
    |id        |      |   |
    |name      |      |   |
    |author_id |      |   |

@ManyToOne

单向多对一

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
@Entity
@Table(name="book")
public class Book {
    
    @Id
    @GeneratedValue
    private Long id;
    /**书名*/
    @Column(length=32)
    private String name;
    @ManyToOne(fetch = FetchType.EAGER)
  	@JoinColumn(name="author_id")
    private Author author;
}

双向关联

多的一方Book不变使用@ManyToOne,一的一方@OneToMany标记中的mappedBy属性的值为Book实体中所引用的Author实体的属性名。mappBy表示关系被维护端,只有关系端有权去更新外键,就是说多端(Book)为关系维护端,负责关系的增删改查。OneToMany默认的加载方式是赖加载。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Entity
@Table(name="author")
public class Author {
    @Id
    @GeneratedValue
    private Long id;
    
    @Column(length=32)
    private String name;
    
   @OneToMany(mappedBy="author")
    private Set<Book> books = new HashSet<Book>();
}

@Entity
@Table(name="book")
public class Book {
    
    @Id
    @GeneratedValue
    private Long id;
    
    @Column(length=32)
    private String name;
    //optional=false,表示author不能为空
    @ManyToOne(cascade = { CascadeType.ALL }, optional=false)
  	//设置在book表中的关联字段(外键)
  	@JoinColumn(name="author_id")
    private Author author;
}

本文为学习记录,因能力有限,如有错误请赐教……如需转载,请注明出处!