Hibernate JPA使用相同的名称连接外键
让我们假设我有:Hibernate JPA使用相同的名称连接外键,hibernate,jpa,orm,Hibernate,Jpa,Orm,让我们假设我有: 表编辑器(id、企业名称、增值税) 表作者(id、姓名、电子邮件) 表书籍(id、标题、描述、fk_作者、fk_编辑) 假设fk_editor是表编辑器id字段的外键,fk_author是表作者id字段的外键 让我们假设图书:作者是n:1和图书:编辑是n:1 问题是:如何通过books表连接这三个表? 这意味着,我必须在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 */
}
表编辑器:
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 */
}
表书籍:
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 */
}
有几件事:
Author
和Editor
类的id
字段不应使用@OneToMany
注释:
@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;
...
}