2017-08-28 86 views
0

假設我有:JPA外鍵連接同名

  1. 編輯(ID,BUSINESS_NAME,增值稅)
  2. 作者(ID,姓名,電子郵件)
  3. 書籍(id,title,description,fk_author,fk_editor)。

比方說fk_editor是表格編輯器的ID字段和fk_author外鍵是表作者的ID字段的外鍵。

假設的關係書:作者N:1周書:編輯N:1

問題是:如何通過書籍表加入三張表表? 這意味着,我必須在Book類中添加哪些代碼才能讓Hibernate理解並與作者和編輯者建立關係?請考慮我在作者和編輯者中擁有相同的id字段名稱。 在這裏它的示例代碼,我需要糾正:

作者

package com.bytecode.jpaexample.SpringBootMySqlJpaRestExample; 

import javax.persistence.*; 
import java.io.Serializable; 

@Entity 
@Table(name = "authors") 
public class Author implements Serializable{ 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id") 
    @OneToMany(fetch = FetchType.LAZY) 
    private int id; 

    @Column(name = "name") 
    private String name; 

    @Column(name = "surname") 
    private String surname; 

    @Column(name = "email") 
    private String email; 

    /* constructors and getters and setters omitted intentionally */ 

} 

編輯

package com.bytecode.jpaexample.SpringBootMySqlJpaRestExample; 

import javax.persistence.*; 
import java.io.Serializable; 

@Entity 
@Table(name = "editors") 
public class Editor implements Serializable{ 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id") 
    @OneToMany(fetch = FetchType.LAZY) 
    private int id; 

    @Column(name = "business_name") 
    private String businessName; 

    @Column(name = "vat") 
    private String vat; 

    /* constructors and getters and setters omitted intentionally */ 

} 

package com.bytecode.jpaexample.SpringBootMySqlJpaRestExample; 

import javax.persistence.*; 
import java.io.Serializable; 

@Entity 
@Table(name="books") 
public class Book implements Serializable{ 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id") 
    private int id; 

    @Column(name = "title") 
    private String title; 

    @Column(name = "description") 
    private String description; 

    @ManyToOne(fetch = FetchType.LAZY)  
    @JoinColumn(name = "id") //authors.id     
    @Column(name = "fk_author") 
    private int fk_editor; 

    @ManyToOne(fetch = FetchType.LAZY)  
    @JoinColumn(name = "id") //editors.id     
    @Column(name = "fk_editor") 
    private int fk_editor; 

    /* constructors and getters and setters omitted intentionally */ 
} 

回答

1

幾件事情:

AuthorEditor類不應該標註爲@OneToMany他們id領域:

@Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id") 
    private int id; 

相反,我猜你想在每一個類另一個領域:

@Entity 
@Table(name = "authors") 
public class Author implements Serializable { 
    ... 
    @OneToMany(mappedBy = "author", fetch = FetchType.LAZY) 
    private List<Book> books; 
    ... 
} 

@Entity 
@Table(name = "editors") 
public class Editor implements Serializable { 
    ... 
    @OneToMany(mappedBy = "editor", fetch = FetchType.LAZY) 
    private List <Book> books; 
    ... 
} 

然後,您需要以幾種方式更改類Book

  • 更適當地重命名字段(例如, fk_author變成author);這些必須與相應類別中的mappedBy設置相匹配,如上所述)
  • 將字段類型更改爲相應的類(例如Author),而不是外鍵類型(例如,int
  • 變化@JoinColumn批註指定在books表(將適當列目標表),並添加referencedColumnName設置(儘管,這些是在這種情況下可選的;因爲它們將默認爲單個主鍵字段目標實體)
  • 刪除@Column註釋

看看這是有道理的:

@Entity 
@Table(name="books") 
public class Book implements Serializable { 
    ... 
    @ManyToOne(fetch = FetchType.LAZY)  
    @JoinColumn(name = "fk_author", referencedColumnName="id") 
    private Author author; 

    @ManyToOne(fetch = FetchType.LAZY)  
    @JoinColumn(name = "fk_editor", referencedColumnName="id") 
    private Editor editor; 
    ... 
}