0

I have modified the design of CPRQ a bit to help my database pattern

I have an Employee table and a Department table. Both have common properties

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

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

So I created a base class ABaseEntity like below

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;

I have extended EmployeeEntity with ABaseEntity

@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;

My CommandHandler runs the following code

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

Database saved the object, but only id, firstname. Does not save tenant and status columns.

I know I am missing something silly. Please help.

EDIT Adding @MappedSuperclass to the ABaseEntity class fixed the issue.

@MappedSuperclass
public class ABaseEntity {...}
Siddharth
  • 9,349
  • 16
  • 86
  • 148

1 Answers1

1

Database saved the object, but only id, firstname. Does not save tenant and status columns.

By default JPA doesn't consider the parent class in the orm (object-relational mapping) of the current class.
You have to specify on the parent class @Inheritance with the strategy to use or use the default one.
For example :

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

More info here.

davidxxx
  • 125,838
  • 23
  • 214
  • 215