我正在 Spring Boot 中创建一个应用程序。我想要一个表有多个实体映射。 一个实体检索没有任何联接的表,另一个实体检索带有联接的表。
这是表架构:
书:{"id":1, "title":"math1", "author_id":1}
作者:{"id":1, "name": "james"}
谢谢。
最佳答案
我成功了。
@MappedSuperclass
public abstract class BookBase<T extends BookBase> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false, updatable = false, name = "id")
private Integer id;
@Column(name = "title")
private String title;
public Integer getId() {
return this.id;
}
public T setId(Integer id) {
this.id = id;
return (T) this;
}
public String getTitle() {
return this.title;
}
public T setTitle(String title) {
this. title = title
return (T) this;
}
}
@Entity
@Table
public class Book extends BookBase<Book> {
@Column(name = "author_id")
private Integer authorId;
public Integer getAuthorId() {
return authorId;
}
public void setAuthorId(Integer authorId) {
this.authorId = authorId;
}
}
@Entity
@Table
public class BookJoinAuthor extends BookBase<BookJoinAuthor> {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "author_id")
private Author author;
public Author getAuthor() {
return author;
}
public void setAuthorId(Author author) {
this.author = author;
}
}
@Entity
@Table
public abstract class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false, updatable = false, name = "id")
private Integer id;
@Column(name = "name")
private String name;
public Integer getId() {
return this.id;
}
public T setId(Integer id) {
this.id = id;
return (T) this;
}
public String getName() {
return this.name;
}
public T setName(String name) {
this. name = name
return (T) this;
}
}
关于java - 如何创建一张表的多个映射? (jpa hibernate ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61892613/