hibernate注解生成表的继承

时间:2022-11-17 12:00:40

在生成表的时候遇到了这样一个问题,将主键放到父类中去,其他的实体表继承主键

@Entity
@Table(name = "base_table")
public class BaseEntity {

	public BaseEntity() {
	}

	@Id
	@GenericGenerator(name = "idGenerator", strategy = "uuid")
	@GeneratedValue(generator = "idGenerator")
	@Column(name = "id", unique = true, nullable = false)
	protected String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

 如果这样,那么生成的主表,会包含其他被继承的表的所有不重复的字段

应改成:

@Entity
@Table(name = "base_table")
@MappedSuperclass
public class BaseEntity {

	public BaseEntity() {
	}

	@Id
	@GenericGenerator(name = "idGenerator", strategy = "uuid")
	@GeneratedValue(generator = "idGenerator")
	@Column(name = "id", unique = true, nullable = false)
	protected String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

 仅仅多加了一个

@MappedSuperclass