【问题标题】:JPARepository CPRQ modified does not save full objectJPARepository CPRQ 修改不保存完整对象
【发布时间】:2021-02-13 15:42:59
【问题描述】:

我稍微修改了 CPRQ 的设计以帮助我的数据库模式

我有一个 Employee 表和一个 Department 表。两者都有共同的属性

@Column(name="tenantIDPKFK")
private Integer tenantIdpkfk;

@Column(name="status")
private Integer status;

所以我创建了一个如下所示的基类 ABaseEntity

public class ABaseEntity {

  public ABaseEntity() {
  }

  public ABaseEntity(int tenantIdpkfk, int status) {
      this.tenantIdpkfk = tenantIdpkfk ;
      this.status = status ;
  }

  @Column(name="tenantIDPKFK")
  private Integer tenantIdpkfk;

  @Column(name="status")
  private Integer status;

我已经用 ABaseEntity 扩展了 EmployeeEntity

@Entity
@Table(name = "employee")
public class EmployeeEntity  extends ABaseEntity{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "first_name")
    @NotEmpty(message = "Please provide a name")
    @NotBlank
    private String firstName;

我的 CommandHandler 运行以下代码

 EmployeeEntity savedEmployeeEntity = this.employeeRepository.saveAndFlush(employee);
 this.mediator.emit(new EmployeeCreatedEvent(savedEmployeeEntity.getId()));

数据库保存了对象,但只有idfirstname。不保存 tenantstatus 列。

我知道我错过了一些愚蠢的东西。请帮忙。

编辑@MappedSuperclass 添加到 ABaseEntity 类解决了这个问题。

@MappedSuperclass
public class ABaseEntity {...}

【问题讨论】:

标签: spring-boot spring-data-jpa


【解决方案1】:

数据库保存了对象,但只有 id、firstname。不保存 租户和状态列。

默认情况下,JPA 不考虑当前类的 orm(对象关系映射)中的父类。
您必须在父类@Inheritance 上指定要使用的策略或使用默认策略。
例如:

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class ABaseEntity {...}

More info here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2011-12-06
    相关资源
    最近更新 更多