0

使用 Microstream 得到以下异常。由引用静态内部类的类引起。我该如何解决这个问题?

异常状态如下,但文档没有解释如何实现PersistenceTypeResolver

详细信息:合成类($1 等)不是可靠的持久性,因为源代码元素的简单重新排序会改变类的名称标识。对于必须依赖于通过标识名称解析类型的类型系统,这将默默地导致潜在的致命错误。如果处理合成类(例如匿名内部类)是绝对必要的,则可以使用自定义 one.microstream.persistence.types.PersistenceTypeResolver 来删除异常并承担正确处理合成类名称的全部责任。在一个.microstream.persistence.types.Persistence.derivePersistentTypeName(Persistence.java:1083)

    public static final AbstractAccompanyingPassenger DUMMY = new AbstractAccompanyingPassenger(MetaDataAccompanyingType.DUMMY) {
        @Override
        public AbstractAccompanyingPassenger clone() {
            return DUMMY;
        }};
Caused by: one.microstream.persistence.exceptions.PersistenceExceptionTypeNotPersistable: Type not persistable: "class net.atpco.metadata.summary.accompanied.MetaDataAccompanying$1". Details: Synthetic classes ($1 etc.) are not reliably persistence since a simple reordering of source code elements would change the name identity of a class. For a type system that has to rely upon resolving types by their identifying name, this would silently cause a potentially fatal error. If handling synthetic classes (e.g. anonymous inner classes) is absolutely necessary, a custom one.microstream.persistence.types.PersistenceTypeResolver can be used to remove the exception and assume  complete responsibility for correctly handling synthetic class names.
    at one.microstream.persistence.types.Persistence.derivePersistentTypeName(Persistence.java:1083)
    at one.microstream.persistence.types.PersistenceTypeResolver.deriveTypeName(PersistenceTypeResolver.java:17)
    at one.microstream.persistence.types.PersistenceTypeHandlerCreator$Abstract.deriveTypeName(PersistenceTypeHandlerCreator.java:73)
    at one.microstream.persistence.binary.types.BinaryTypeHandlerCreator$Default.internalCreateTypeHandlerGeneric(BinaryTypeHandlerCreator.java:238)
    at one.microstream.persistence.types.PersistenceTypeHandlerCreator$Abstract.createTypeHandlerGeneric(PersistenceTypeHandlerCreator.java:168)
    at one.microstream.persistence.types.PersistenceTypeHandlerEnsurer$Default.ensureTypeHandler(PersistenceTypeHandlerEnsurer.java:199)
    at one.microstream.persistence.internal.PersistenceTypeHandlerProviderCreating.ensureTypeHandler(PersistenceTypeHandlerProviderCreating.java:170)
    at one.microstream.persistence.internal.PersistenceTypeHandlerProviderCreating.provideTypeHandler(PersistenceTypeHandlerProviderCreating.java:78)
    at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.internalEnsureTypeHandler(PersistenceTypeHandlerManager.java:587)
    at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.ensureTypeHandler(PersistenceTypeHandlerManager.java:357)
    at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.ensureTypeHandler(PersistenceTypeHandlerManager.java:333)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.registerGuaranteed(BinaryStorer.java:557)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.registerLazyOptional(BinaryStorer.java:572)
    at one.microstream.persistence.types.PersistenceObjectManager$Default.ensureObjectId(PersistenceObjectManager.java:182)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.register(BinaryStorer.java:591)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.apply(BinaryStorer.java:298)
    at one.microstream.persistence.binary.types.BinaryValueFunctions$9.storeValueFromMemory(BinaryValueFunctions.java:147)
    at one.microstream.persistence.binary.types.Binary.storeFixedSize(Binary.java:1149)
    at one.microstream.persistence.binary.internal.AbstractBinaryHandlerReflective.store(AbstractBinaryHandlerReflective.java:497)
    at one.microstream.persistence.binary.internal.AbstractBinaryHandlerReflective.store(AbstractBinaryHandlerReflective.java:1)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.storeItem(BinaryStorer.java:414)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.storeGraph(BinaryStorer.java:403)
    at one.microstream.persistence.binary.types.BinaryStorer$Default.store(BinaryStorer.java:421)
    at one.microstream.persistence.types.PersistenceManager$Default.store(PersistenceManager.java:274)
    at one.microstream.storage.types.StorageConnection.store(StorageConnection.java:306)
    at one.microstream.cache.CacheStore$Default.write(CacheStore.java:112)

4

2 回答 2

1

由于异常状态不支持持久合成类。在您的情况下,重构您的类以摆脱那些合成类是唯一可靠的选择。

于 2020-08-06T11:32:26.873 回答
0

我找到了解决此问题的方法,而无需更改 Java 对象图结构并具有合成类/静态引用。

  1. 为具有合成类/静态引用的对象实现CustomBinaryHandler
  2. 覆盖存储方法
  3. 覆盖创建方法
  4. 覆盖initializeState方法

店铺

如果引用是合成类/静态引用,则创建对象的新实例并将引用设置为“标记”。在我的情况下为空。不要更新传递给store方法的当前对象,确保创建一个克隆并修改适用的引用。然后调用super.store()

创造

创建对象的空实例

初始化状态

  1. 检索对象的值
  2. 如果有问题的值具有“标记”,则将其设置为合成类/静态引用
  3. 创建一个克隆对象并填充值
  4. XReflect.copyFields 从克隆到真实实例
public class ExampleTypeHandler extends CustomBinaryHandler<Example> {

    private static Class<Example> handledType() {
        return Example.class; // to get ".class" to work
    }
    
    //the fields to be persisted
    BinaryField<Example> accompanying = Field(Object.class, Example::getAccompanying);
    BinaryField<Example> resulting = Field(Object.class, Example::getResulting);
    
    public ExampleTypeHandler()
    {
        super(handledType());
    }
    
    @Override
    public void store(Binary data, Example dtl, long objectId, PersistenceStoreHandler<Binary> handler) {
        if (dtl.getAccompanying() == MetaDataAccompanying.DUMMY) {
            dtl = Example.of(dtl.getResulting(), null);
        }
        super.store(data, dtl, objectId, handler);
    }
    
    @Override
    public Example create(final Binary data, final PersistenceLoadHandler handler) {
        return Example.of(null, null);
    }

    @Override
    public void initializeState(final Binary data, final Example instance, final PersistenceLoadHandler handler) {
        //get the referenced Objects
        Object accompanying = this.accompanying.readReference(data, handler);
        String r = (String)this.resulting.readReference(data, handler);
            
        if (accompanying == null) {
            accompanying = MetaDataAccompanying.DUMMY;
        }

        MetaDataAccompanying a = (MetaDataAccompanying) accompanying;
        Example dtl = Example.of(r, a);
        
        XReflect.copyFields(dtl, instance);
    }       
    
}
于 2020-08-07T15:15:04.590 回答