从Spring Data Mongo中的继承关系中的特定实体检索数据

时间:2017-08-14 12:49:56

标签: spring mongodb spring-boot spring-data spring-data-mongodb

我使用Spring Data MongoDB实现了继承关系

我有一个抽象实体,其中包含所有其他属性共有的所有属性。

@Document(collection = PersonEntity.COLLECTION_NAME)
public abstract class PersonEntity {

    public final static String COLLECTION_NAME = "persons";

    @Id
    private ObjectId id;

    @Field("first_name")
    private String firstName;

    @Field("last_name")
    private String lastName;

    private Integer age;


    public PersonEntity(){}

    @PersistenceConstructor
    public PersonEntity(String firstName, String lastName, Integer age) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }


    public ObjectId getId() {
        return id;
    }


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getFullName(){
        return this.firstName + " - " + this.lastName;
    }

}

此实体继承实体UserSystemEntity和SonEntity。它们具有特定的 @Field 属性,并且不定义 @Document 注释。所以所有文件都存储在“PERSONS”的集合中。

为了使用这些实体,我创建了相应的存储库。在这里,我为实体 SonEntity

设置了存储库
@Repository
public interface SonRepository extends MongoRepository<SonEntity, ObjectId> {
    Iterable<SonEntity> findByParentId(ObjectId id);
    Long countByParentId(ObjectId id);
    Long countByParentIdAndId(ObjectId parentId, ObjectId id);
}

我遇到的问题是,当我使用此存储库通过以下方法获取实体“SonEntity”列表时:

@Override
    public Page<SonDTO> findPaginated(Pageable pageable) {
        Page<SonEntity> childrenPage = sonRepository.findAll(pageable);
        return childrenPage.map(new Converter<SonEntity, SonDTO>(){
            @Override
            public SonDTO convert(SonEntity sonEntity) {
               return sonEntityMapper.sonEntityToSonDTO(sonEntity);
            }
        });

它返回所有实体类型的文档(UserSystemEntity,ParentEntity,SonEntity)。

如何正确配置以仅检索SonEntity实体的文档?

提前致谢。

“SonEntity”实体代码:

public final class SonEntity extends PersonEntity {

    @DBRef
    private SchoolEntity school;
    @DBRef
    private ParentEntity parent;

    public SonEntity() {
    }

    @PersistenceConstructor
    public SonEntity(String firstName, String lastName, Integer age, SchoolEntity school, ParentEntity parent) {
        super(firstName, lastName, age);
        this.school = school;
        this.parent = parent;
    }

    public SchoolEntity getSchool() {
        return school;
    }

    public void setSchool(SchoolEntity school) {
        this.school = school;
    }

    public ParentEntity getParent() {
        return parent;
    }

    public void setParent(ParentEntity parent) {
        this.parent = parent;
    }

}

“UserSystemEntity”代码:

public class UserSystemEntity extends PersonEntity {

    @Field("email")
    protected String email;

    @Field("password")
    protected String password;

    @Field("is_locked")
    protected Boolean locked = Boolean.FALSE;

    @Field("last_login_access")
    protected Date lastLoginAccess;

    @DBRef
    protected AuthorityEntity authority;

    public UserSystemEntity() {
    }

    @PersistenceConstructor
    public UserSystemEntity(String firstName, String lastName, Integer age, String email, String password, AuthorityEntity authority) {
        super(firstName, lastName, age);
        this.email = email;
        this.password = password;
        this.authority = authority;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Boolean isLocked() {
        return locked;
    }

    public void setLocked(Boolean locked) {
        this.locked = locked;
    }

    public Date getLastLoginAccess() {
        return lastLoginAccess;
    }

    public void setLastLoginAccess(Date lastLoginAccess) {
        this.lastLoginAccess = lastLoginAccess;
    }

    public AuthorityEntity getAuthority() {
        return authority;
    }

    public void setAuthority(AuthorityEntity authority) {
        this.authority = authority;
    }

    @Override
    public String toString() {
        return "UserSystemEntity [email=" + email + ", password=" + password + ", locked=" + locked + ", authority="
                + authority + "]";
    }
}

“ParentEntity”代码:

public final class ParentEntity extends UserSystemEntity {

    public ParentEntity() {
    }

    @PersistenceConstructor
    public ParentEntity(String firstName, String lastName, Integer age, String email, String password,
            AuthorityEntity authority) {
        super(firstName, lastName, age, email, password, authority);
    }

}

在这里,您可以验证在尝试列出“SonEntity”类型的所有实体时,会显示其他实体的数据。用户“admin”作为 UserSystemEntity 类型的文档存储在数据库中:

son entities

信息存储在MongoDB中,如下所示:

Information on MongoDB

0 个答案:

没有答案