Spring REST 存储库继承空值

Spring REST Repositories inheritance null values

我在映射 parent-child classes 到 Spring Rest Repositories 时遇到了一些问题。我的情况是,我需要使用 Spring 安全性对两种类型的用户进行身份验证,但我还希望能够将特定类型的属性(如宠物)分配给所有者,将薪水分配给员工。这是实现这样目标的好结构吗?如果是这样,请帮助解决这个问题。

我已经将它插入到一个 table 中,用 dtype 列区分彼此,但无法插入除 dtype 之外的任何其他值。

我在 MS SQL.

中使用最新的 Spring 引导(默认 JSON 映射器)

这是我 POST 请求的负载:

{
    "username": "admin",  // null in db
    "firstName": "Delacruz", // null in db
    "lastName": "House", // null in db
    "dtype": "Owner" // present in db
}

这是我的 class 结构:
存储库

@RepositoryRestResource(path = "user", collectionResourceRel = "user")
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
}

Parent class - 用户

@Entity
@Table(name = "users")
@Inheritance
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
        include=JsonTypeInfo.As.EXISTING_PROPERTY,
        property="dtype")
@JsonSubTypes({
        @JsonSubTypes.Type(name="Owner", value=Owner.class),
        @JsonSubTypes.Type(name="Employee", value=Employee.class)})
@RestResource(path="user")
public abstract class User{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String username;
// rest of properties
}

员工 - 第一个 child class

@EqualsAndHashCode(callSuper = true)
@Entity
@Data
public class Employee extends User {

    private String type = "EMPLOYEE";

    @Column
    @Enumerated
    private EmployeeType employeeType;

    @Column
    private LocalDate dateOfEmployment;
    //rest of properties
}

所有者 - 另一个 child class

@EqualsAndHashCode(callSuper = true)
@Data
@Entity
public class Owner extends User{
    @OneToMany(mappedBy = "owner")
    private List<Pet> pets;
}

向子类添加以下构造函数(根据所有者)解决了问题:

@JsonCreator
public Employee(
        @JsonProperty(value = "username") String username,
        @JsonProperty(value = "firstName") String firstName,
        @JsonProperty(value = "lastName") String lastName,
        @JsonProperty(value = "password") String password,
        @JsonProperty(value = "email") String email,
        @JsonProperty(value = "phoneNumber") String phoneNumber
) {
    super(username, firstName, lastName, password, email, phoneNumber);
}