簡體   English   中英

Spring REST存儲庫繼承null值

[英]Spring REST Repositories inheritance null values

我在Spring Rest存儲庫中映射父子類時遇到了一些麻煩。 我的情況是,我需要使用Spring Security驗證兩種類型的用戶,但是我還希望能夠為類型分配特定於屬性的屬性,例如Pets to Owner和Employee的薪水。 是實現這個目標的良好結構嗎? 如果是這樣,請幫助解決該問題。

我已經將它插入到一個表中,並用dtype列將它們彼此區別開來,但是無法插入除dtype之外的任何其他值。

我正在使用最新的Spring Boot(默認JSON映射器)和MS SQL。

這是我的POST請求有效負載:

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

這是我的課程結構:
資料庫

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

家長班-用戶

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

員工-第一胎班

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

主人-另一個孩子班

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM