SpringBoot和MongoDB如何抽象相似的实体

时间:2019-02-08 10:09:44

标签: java spring-boot spring-mongodb

我的应用程序具有两个结构相当相似的数据库集合。现在,Sonar在我的两个处理这些集合实体的控制器之间有一些代码重复。因此,我的目标是让一个抽象控制器来处理数据库交互,但是目前我不知道如何实现此目标。

这是我的两个实体类及其共享的接口(省略了一些设置器):

public interface Auditable {

    Audit getAudit();

    void setAudit(Audit audit);
}

public interface BinaryEntity extends Auditable {

    String getId();

    String getEntityId();

    void setEntityId(String id)

    int getVersion();

    void setVersion(int version);

    String getVersionDescription();

    String getFileName();

    MetaData getMetaData();

    Binary getFile();

}

@Data
@Document(collection = "files")
public class FileEntity implements BinaryEntity {

    @Id
    private String id;

    private String fileId;
    private int version;
    private String versionDescription;

    private String fileName;
    private String mimeType;
    private MetaData metaData;
    private Audit audit;

    private Binary file;

    public FileEntity(String fileName, String mimeType) {
        this.fileName = fileName;
        this.mimeType = mimeType;
    }

    @Override
    public String getEntityId() {
        return fileId;
    }

    @Override
    public void setEntityId(String id) {
        this.fileId = id;
    }
}

@Data
@Document(collection = "forms")
public class FormEntity implements BinaryEntity {

    @Id
    private String id;

    private String formId;
    private int version;
    private String versionDescription;

    private String fileName;
    private MetaData metaData;
    private PdfSettings settings;
    private Audit audit;
    private List<Mapping> mappings;

    private Binary file;
    private String password;

    public FormEntity(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public String getEntityId() {
        return formId;
    }

    @Override
    public void setEntityId(String id) {
        this.formId = id;
    }

}

现在让我们从插入开始。我尝试了以下操作,但是在尝试调用它时,出现了编译错误,尽管FileRepository extends MongoRepository<FileEntity, String>出现了“ BinaryController不适用于参数(用户,MultipartFile,FileRepository)”:

public ResponseEntity<Object> uploadFile(User principal, MultipartFile fileParam,
            MongoRepository<BinaryEntity, String> repository) {
        try {
            BinaryEntity binary = createFormFromMultipart(fileParam);
            binary.setAudit(Audit.buildCreated(userService.getUser(principal)));
            repository.insert(binary);
            binary.setEntityId(binary.getId());
            binary.setVersion(1);
            repository.save(binary);
            return ResponseUtils.sendResponseObjectAsJson(binary, HttpStatus.OK);
        } catch (IOException e) {
            LOGGER.error(COULD_NOT_UPLOAD + ": ", e);
            return ResponseUtils.sendResponseObjectAsJson(COULD_NOT_UPLOAD_MESSAGE, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

这甚至可以实现吗?如果可以,我如何实现呢?

编辑: 由于我仍然没有得到任何答案,甚至没有评论,所以我玩了一段时间,并管理了部分抽象。所有repository方法仍然在继承控制器中,并包装在抽象方法中:

abstract BinaryEntity createFormFromMultipart(MultipartFile fileParam) throws IOException;

abstract BinaryEntity insert(BinaryEntity entity);

abstract BinaryEntity save(BinaryEntity entity);

abstract Optional<? extends BinaryEntity> findById(String dbId);

abstract Optional<? extends BinaryEntity> findLatestVersion(String entityId);

abstract Optional<? extends BinaryEntity> findByVersion(String entityId, int version);

但是,仍然必须有一种方法可以为抽象控制器提供通用的MongoRepository

0 个答案:

没有答案