Spring数据couchbase文件java类继承

时间:2018-04-05 10:23:05

标签: java spring inheritance couchbase spring-data-couchbase

我的问题是,spring数据库不会搜索搜索类的子类。例如:

型号:

@Document 
class A { 
   @Id
   String id 
}

@Document
class B extends A {}

和存储库:

public interface ARepository extends PagingAndSortingRepository<A, String>{
     Page<A> findAll(Pageable pageable);
}

Spring数据couchbase生成查询,具有where where condition

  

_class =&#34; com.example.model.A&#34;

但是我想在这个查询中搜索B文件。在某种程度上,我该怎么做?当我编写自己的查询时,我必须在查询中定义顺序,限制和偏移,并且不使用Pageable。但我想使用Pageable。

2 个答案:

答案 0 :(得分:0)

考虑基于继承的通用接口。

首先创建超类:

import Foundation

enum AcModeType {
    case acModeCool
    case acModeDry
    case acModeHeat
    case acModeFan
    case acModeTimer
}

enum FanModeType {
    case fanModeLow
    case fanModeMid
    case fanModeHigh
    case FanModeAuto
}

class AcDevice: Device {
    var brandId: String?
    var variant: String?
    var brandName: String?
    var acTemperature: Int = 16
    var acMinTemperature: Int = 16
    var acMaxTemperature: Int = 32
    var weatherTemperature: Int = 0
    var roomTemperature: Int = 0
    var timeTable = [TimeTable]()
    var currentAcMode: AcModeType = .acModeCool
    var currentFanMode: FanModeType = .fanModeLow
    var lastPowerOnTime: Double = 0.0
    var isPowerOn = false
    var isSleepOn = false
    var isTimeTableOn = false
    var hasScreen = false

    required public init?(coder aDecoder: NSCoder) {
        super.init();
        brandId             =   (aDecoder.decodeObject(forKey: "brandId") as! String?)
        variant             =   (aDecoder.decodeObject(forKey: "variant") as! String?)
        brandName           =   (aDecoder.decodeObject(forKey: "brandName") as! String?)
        acTemperature       =   (aDecoder.decodeObject(forKey: "temperature") as! Int)
        acMinTemperature    =   (aDecoder.decodeObject(forKey: "acMinTemperature") as! Int )
        acMaxTemperature    =   (aDecoder.decodeObject(forKey: "acMaxTemperature") as! Int )
        weatherTemperature  =   (aDecoder.decodeObject(forKey: "weatherTemperature") as! Int)
        roomTemperature     =   (aDecoder.decodeObject(forKey: "roomTemperature") as! Int )
        currentAcMode       =   (aDecoder.decodeObject(forKey: "currentAcMode") as! AcModeType)
        currentFanMode      =   (aDecoder.decodeObject(forKey: "currentFanMode") as! FanModeType)
        lastPowerOnTime     =   (aDecoder.decodeObject(forKey: "lastPowerOnTime") as! Double)
        if let timeTable    =   aDecoder.decodeObject(forKey: "timeTable") as! [TimeTable]? {self.timeTable=timeTable}
    }

    public override func encode(with aCoder: NSCoder) {
        aCoder.encode(self.brandId, forKey: "brandId")
        aCoder.encode(self.variant, forKey: "variant")
        aCoder.encode(self.brandName, forKey: "brandName")
        aCoder.encode(self.acTemperature, forKey: "temperature")
        aCoder.encode(self.timeTable, forKey: "timeTable")
        aCoder.encode(self.acMinTemperature, forKey: "acMinTemperature")
        aCoder.encode(self.acMaxTemperature, forKey: "acMaxTemperature")
        aCoder.encode(self.weatherTemperature, forKey: "weatherTemperature")
        aCoder.encode(self.roomTemperature, forKey: "roomTemperature")
        aCoder.encode(self.currentAcMode, forKey: "currentAcMode")
        aCoder.encode(self.currentFanMode, forKey: "currentFanMode")
        aCoder.encode(self.lastPowerOnTime, forKey: "lastPowerOnTime")
    }

    required public init() {
        super.init();
        brandId = ""
        variant = ""
        brandName = ""

        isPowerOn = false;
        isSleepOn = false;
        isTimeTableOn = false;
        hasScreen = false;
        currentAcMode = .acModeCool
        acMinTemperature = 16;
        acMaxTemperature = 32;
        currentFanMode = .fanModeLow
        lastPowerOnTime = -1;
        timeTable = [TimeTable]()
    }
}

然后创建子类:

@Inheritance
public abstract class SuperClass{ 

  @Id
  private int id;
}

创建基础存储库:

public class A extends SuperClass { /* ... */ }
public class B extends SuperClass { /* ... */ }

}

然后根据基础仓库创建SuperClass存储库:

@NoRepositoryBean
public interface SuperClassBaseRepository<T extends SuperClass> 
extends PagingAndSortingRepository<T, Integer> { 
     public T findAll();

SuperClassRepository @Transactional public interface SuperClassRepository extends SuperClassBaseRepository<SuperClass> { /* ... */ } @Transactional public interface ARepository extends SuperClassBaseRepository<A> { /* ... */ } @Transactional public interface BRepository extends SuperClassBaseRepository<B> { /* ... */ } 将搜索所有A和B类

答案 1 :(得分:0)

我们设法在 Spring Data Couchbase 3.2.12 上完成了这项工作。这是我们所做的:

我们发现,只有当该类型存在存储库时,才会为每种类型创建映射器,因此,除了我们的超类存储库...

public interface ARepository extends PagingAndSortingRepository<A, String> {
     Page<A> findAll(Pageable pageable);
}

我们为每个子类型创建了一个空存储库,例如:

public interface BRepository extends PagingAndSortingRepository<B, String>{
     // No methods
}

第二个 repo 的存在保证了 B 的适当映射器的存在,因此当在 findAll 中调用 ARepository(或其他方法)时,每个子类的映射器是当下。完成此操作后,我们能够获得实际为 A 实例的 B 列表。

希望这会有所帮助,没有人需要在此上浪费更多时间。 :)