java - 如何创建一张表的多个映射? (jpa hibernate )

标签 java hibernate jpa

我正在 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/

相关文章:

java - 基本类型的一对一映射,到 Hibernate 中的单个类

java - 异常后丢失EntityManager

java - 图像像素操作 : Use native APIs or 3rd party libraries?

java - GET Activity 和地点 - EntryPoint 中的面板

java - 是否可以使用 Hibernate、JIBX 等技术自动生成带注释的映射 POJO 类?

java - JPA - 来自 hibernate 的 LobCreator 模拟?

java - 如何从 JSP 页面保存 JPA 实体模型属性的特定属性

java - 方法不覆盖抽象父类(super class)

java se 与 hibernate 和 JavaDB。我如何部署应用程序?

java - hibernate 异常 : detached entity passed to persist