Hibernate实体扩展基类,为实体形成的表不具有基类中属性的列

时间:2016-02-02 01:07:45

标签: java 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。如何获得包括作者在内的所有三位成员的表格。

我的sql表只有两列,bookId和title。如何获得包括作者在内的所有三位成员的表格。

1 个答案:

答案 0 :(得分:9)

您应该将@MappedSuperclass注释添加到BaseEntity

@MappedSuperclass 
public class BaseEntity {

  @Column
  private String author;

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

  public String getAuthor() {
      return author;
  }

}