Hibernate实体扩展基类,为实体形成的表没有基类中的属性列 [英] Hibernate entity extending base class, table formed for the entity doesn't have columns for attributes in base class

查看:105
本文介绍了Hibernate实体扩展基类,为实体形成的表没有基类中的属性列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class BaseEntity {

  @Column
  private String author;

  public BaseEntity(String author) {
      this.author = author;
  }

  public String getAuthor() {
      return author;
  }
}

@Entity
@Table(name = "books")
public class Book extends BaseEntity {

  @Id
  @GeneratedValue(generator = "increment")
  @GenericGenerator(name = "increment", strategy = "increment")
  private long bookId;

  @Column
  private String title;

  public Book(String author, String title) {
    super(author);
    this.title = title;
  }

  public Book() {
    super("default");
  }

  public String getTitle() {
    return title;
  }
}

我的sql表只有两列,bookId和title 。我该怎么做才能获得包括作者在内的所有三个成员的表格。

My sql table have just two columns, bookId and title. What should I do to get table for all the three members including author.

我的sql表只有两列,bookId和title。我应该怎样做才能获得包括作者在内的所有三个成员的表格。

My sql table have just two columns, bookId and title. What should I do to get table for all the three members including author.

推荐答案

你应该添加 @ MappedSuperclass 注释到 BaseEntity

@MappedSuperclass 
public class BaseEntity {

  @Column
  private String author;

  public BaseEntity(String author) {
      this.author = author;
  }

  public String getAuthor() {
      return author;
  }

}

这篇关于Hibernate实体扩展基类,为实体形成的表没有基类中的属性列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆