英文:
Java Microstream - ImmutablePair causing PersistenceExceptionTypeNotPersistable
问题
Apache ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)在存储Java对象图时引发了PersistenceExceptionTypeNotPersistable。
我该如何解决这个问题?如果需要使用typeHandler,是否有示例?
Caused by: one.microstream.persistence.exceptions.PersistenceExceptionTypeNotPersistable: Type not persistable: "class org.apache.commons.lang3.tuple.ImmutablePair".
at one.microstream.persistence.binary.internal.BinaryHandlerUnpersistable.guaranteeSpecificInstanceViability(BinaryHandlerUnpersistable.java:66)
at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.ensureTypeHandler(PersistenceTypeHandlerManager.java:336)
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.Binary.storeIterableContentAsList(Binary.java:1698)
at one.microstream.persistence.binary.types.Binary.storeIterableAsList(Binary.java:668)
at one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustomIterableSimpleListElements.store(AbstractBinaryHandlerCustomIterableSimpleListElements.java:64)
at one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustomIterableSimpleListElements.store(AbstractBinaryHandlerCustomIterableSimpleListElements.java:1)
at one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustom.store(AbstractBinaryHandlerCustom.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)
英文:
Apache ImmutablePair, (org.apache.commons.lang3.tuple.ImmutablePair) causing PersistenceExceptionTypeNotPersistable when storing Java Object graph.
How should I solve this? If with a typeHandler, is there an example?
Caused by: one.microstream.persistence.exceptions.PersistenceExceptionTypeNotPersistable: Type not persistable: "class org.apache.commons.lang3.tuple.ImmutablePair".
at one.microstream.persistence.binary.internal.BinaryHandlerUnpersistable.guaranteeSpecificInstanceViablity(BinaryHandlerUnpersistable.java:66)
at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.ensureTypeHandler(PersistenceTypeHandlerManager.java:336)
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.Binary.storeIterableContentAsList(Binary.java:1698)
at one.microstream.persistence.binary.types.Binary.storeIterableAsList(Binary.java:668)
at one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustomIterableSimpleListElements.store(AbstractBinaryHandlerCustomIterableSimpleListElements.java:64)
at one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustomIterableSimpleListElements.store(AbstractBinaryHandlerCustomIterableSimpleListElements.java:1)
at one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustom.store(AbstractBinaryHandlerCustom.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)
答案1
得分: 0
以下是直接由MicroStream共享的答案,我可以确认这个答案完美地工作。
你好,
如果一个类被视为不可持久化的,可以为这些类提供自定义类型处理程序的实现。在你的情况下,自定义类型处理程序可能如下所示:
import org.apache.commons.lang3.tuple.ImmutablePair;
import one.microstream.persistence.binary.internal.CustomBinaryHandler;
import one.microstream.persistence.binary.types.Binary;
import one.microstream.persistence.binary.types.BinaryField;
import one.microstream.persistence.types.PersistenceLoadHandler;
import one.microstream.reflect.XReflect;
public class ImmutablePairTypeHandler extends CustomBinaryHandler<ImmutablePair<?, ?>>
{
@SuppressWarnings({"unchecked", "rawtypes"})
private static Class<ImmutablePair<?, ?>> handledType()
{
return (Class) ImmutablePair.class; // to get ".class" to work
}
// the fields to be persisted
BinaryField<ImmutablePair<?, ?>> left = Field(Object.class, ImmutablePair::getLeft);
BinaryField<ImmutablePair<?, ?>> right = Field(Object.class, ImmutablePair::getRight);
public ImmutablePairTypeHandler()
{
super(handledType());
}
@Override
public ImmutablePair<?, ?> create(final Binary data, final PersistenceLoadHandler handler)
{
// create an empty instance, don't use nullPair() as it is a static final
return ImmutablePair.of(null, null);
}
@Override
public void initializeState(final Binary data, final ImmutablePair<?, ?> instance, final PersistenceLoadHandler handler)
{
// get the referenced Objects
final Object left = this.left.readReference(data, handler);
final Object right = this.right.readReference(data, handler);
// as the pair is immutable we can't set the pair's fields directly
final ImmutablePair<?, ?> pair = ImmutablePair.of(left, right);
XReflect.copyFields(pair, instance);
}
}
要启用此处理程序,只需注册它:
final EmbeddedStorageFoundation<?> foundation = EmbeddedStorage.Foundation(workdir);
foundation.registerTypeHandler(new ImmutablePairTypeHandler());
final EmbeddedStorageManager storage = foundation.start();
英文:
Below is the answer per shared by microstream directly, and I can confirm this works perfectly.
Hello,
If a class is considered as unpersitable it is possible to provide a custom type handler implementation for such classes. In your case a custom type handler may look like:
import org.apache.commons.lang3.tuple.ImmutablePair;
import one.microstream.persistence.binary.internal.CustomBinaryHandler;
import one.microstream.persistence.binary.types.Binary;
import one.microstream.persistence.binary.types.BinaryField;
import one.microstream.persistence.types.PersistenceLoadHandler;
import one.microstream.reflect.XReflect;
public class ImmutablePairTypeHandler extends CustomBinaryHandler<ImmutablePair<?, ?>>
{
@SuppressWarnings({"unchecked", "rawtypes"})
private static Class<ImmutablePair<?, ?>> handledType()
{
return (Class)ImmutablePair.class; // to get ".class" to work
}
//the fields to be persisted
BinaryField<ImmutablePair<?, ?>> left = Field(Object.class, ImmutablePair::getLeft);
BinaryField<ImmutablePair<?, ?>> right = Field(Object.class, ImmutablePair::getRight);
public ImmutablePairTypeHandler()
{
super(handledType());
}
@Override
public ImmutablePair<?, ?> create(final Binary data, final PersistenceLoadHandler handler)
{
//create a empty instance, don't use nullPair() as it is a static final
return ImmutablePair.of(null, null);
}
@Override
public void initializeState(final Binary data, final ImmutablePair<?, ?> instance, final PersistenceLoadHandler handler)
{
//get the referenced Objects
final Object left = this.left.readReference(data, handler);
final Object right = this.right.readReference(data, handler);
//as the pair is immutable we can't set the pairs fields directly
final ImmutablePair<?,?> pair = ImmutablePair.of(left, right);
XReflect.copyFields(pair, instance);
}
}
To enable this handler just register it:
final EmbeddedStorageFoundation<?> foundation = EmbeddedStorage.Foundation(workdir);
foundation.registerTypeHandler(new ImmutablePairTypeHandler());
final EmbeddedStorageManager storage = foundation.start();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论