Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将自定义属性类型注册到xodus`PersistentEntityStore`_Java_Xodus - Fatal编程技术网

Java 如何将自定义属性类型注册到xodus`PersistentEntityStore`

Java 如何将自定义属性类型注册到xodus`PersistentEntityStore`,java,xodus,Java,Xodus,我正在使用xodus数据库,我想将java.time.Instant存储为我的模型对象(Diary)的属性,所以我实现了InstantBinding 下一个是我创建的存储库对象,用于处理我在存储库的保存和加载方法中注册的InstantBing对象,因为PersistentEntityStore.registerCustomPropertyType(@NotNull final StoreTransaction txn,@NotNull final Class您似乎正在体验。它已经修复,该修复将在

我正在使用xodus数据库,我想将
java.time.Instant
存储为我的模型对象(
Diary
)的属性,所以我实现了
InstantBinding


下一个是我创建的存储库对象,用于处理我在存储库的保存和加载方法中注册的
InstantBing
对象,因为
PersistentEntityStore.registerCustomPropertyType(@NotNull final StoreTransaction txn,@NotNull final Class您似乎正在体验。它已经修复,该修复将在版本1.0.6中提供。目前有两种解决方法:

  • 用try-catch块包围persistenentEntityStore.registerCustomPropertyType()
  • 调用,catch
    EntityStoreException
    并忽略它。下一个版本(1.0.6)可用时,使项目依赖它,并删除try-catch块
  • 根据发布到的
    1.0.6-SNAPSHOT
    版本设置项目

  • DiariesView#saveChanges()
    是否创建事务?@vyacheslav-lukianov事务将在
    DiaryRepository.saveDiary
    方法中创建。我不知道为什么
    registerCustomPropertyType
    需要事务作为参数?!
    Exception in thread "JavaFX Application Thread" jetbrains.exodus.entitystore.EntityStoreException: Already registered property type id 9
    at jetbrains.exodus.entitystore.tables.PropertyTypes.registerCustomPropertyType(PropertyTypes.java:77)
    at jetbrains.exodus.entitystore.PersistentEntityStoreImpl.registerCustomPropertyType(PersistentEntityStoreImpl.java:520)
    at io.github.mojtab23.diaries.DiaryRepository.lambda$saveDiary$0(DiaryRepository.java:35)
    at jetbrains.exodus.entitystore.PersistentEntityStoreImpl.executeInTransaction(PersistentEntityStoreImpl.java:529)
    at io.github.mojtab23.diaries.DiaryRepository.saveDiary(DiaryRepository.java:34)
    at io.github.mojtab23.diaries.DiariesView.saveChanges(DiariesView.java:222)
    at io.github.mojtab23.diaries.DiariesView.lambda$buildDiaryEditor$6(DiariesView.java:203)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
    
    import javafx.beans.property.ObjectProperty;
    import javafx.beans.property.SimpleObjectProperty;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    import jetbrains.exodus.entitystore.EntityId;
    
    public class Diary implements Cloneable {
    
    private final ObjectProperty<Instant> timestamp;
    private final StringProperty text;
    private EntityId id;
    
    public Diary() {
        this.text = new SimpleStringProperty();
        timestamp = new SimpleObjectProperty<>(Instant.now());
    }
    
    public Diary(String text, Instant time) {
        this.text = new SimpleStringProperty(text);
        timestamp = new SimpleObjectProperty<>(time);
    
    }
    
    public Diary(SimpleObjectProperty<Instant> timestamp, StringProperty text, EntityId id) {
        this.timestamp = timestamp;
        this.text = text;
        this.id = id;
    }
    
    public EntityId getId() {
        return id;
    }
    
    public void setId(EntityId id) {
        this.id = id;
    }
    
    public Instant getTimestamp() {
        return timestamp.get();
    }
    
    public void setTimestamp(Instant timestamp) {
        this.timestamp.set(timestamp);
    }
    
    public ObjectProperty<Instant> timestampProperty() {
        return timestamp;
    }
    
    public String getText() {
        return text.get();
    }
    
    public void setText(String text) {
        this.text.set(text);
    }
    
    public StringProperty textProperty() {
        return text;
    }
    
    @Override
    public String toString() {
        return "Diary{" +
                "id=" + id +
                ", timestamp=" + timestamp +
                ", text=" + text +
                '}';
    }
    
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    }
    
    import io.github.mojtab23.diaries.model.diary.Diary;
    import jetbrains.exodus.entitystore.*;
    import org.jetbrains.annotations.Nullable;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Repository;
    
    import javax.annotation.PreDestroy;
    import java.time.Instant;
    import java.util.ArrayList;
    import java.util.List;
    
    
    
    @Repository
    public class DiaryRepository {
        private static final Logger LOGGER = LoggerFactory.getLogger(DiaryRepository.class);
    
        private final PersistentEntityStore entityStore;
        private final String entityType = "Diary";
    
        public DiaryRepository() {
            entityStore = PersistentEntityStores.newInstance(".DiariesAppData");
    
    
        }
    
    
        public void saveDiary(final Diary diary) {
            entityStore.executeInTransaction((StoreTransaction txn) -> {
                entityStore.registerCustomPropertyType(txn, Instant.class, InstantBinding.BINDING);
    
                diaryToEntity(diary, txn);
            });
    
        }
    
        public Entity diaryToEntity(final Diary diary, final StoreTransaction txn) {
    
            final Entity diaryEntity = txn.newEntity(entityType);
            diaryEntity.setProperty("text", diary.getText());
    
            diaryEntity.setProperty("timestamp", diary.getTimestamp());
            return diaryEntity;
        }
    
    
        public Diary entityToDiary(final Entity entity, final StoreTransaction txn) {
    
    
            final @Nullable String text = (String) entity.getProperty("text");
            final @Nullable Instant timestamp = (Instant) entity.getProperty("timestamp");
    
            if (text != null) {
                if (timestamp != null) {
                       return new Diary(text, timestamp);
                } else
                    return new Diary(text, Instant.MIN);
            }
            return null;
        }
    
        public List<Diary> readAllDiaries() {
    
            return entityStore.computeInReadonlyTransaction(txn -> {
                entityStore.registerCustomPropertyType(txn, Instant.class, InstantBinding.BINDING);
    
                final EntityIterable allDiaries = txn.getAll(entityType);
                   final List<Diary> diaryList = new ArrayList<>();
                allDiaries.forEach(entity -> {
                    final Diary e = entityToDiary(entity, txn);
                    if (e != null) {
                        diaryList.add(e);
                    }
                });
                LOGGER.warn("Number of Diaries: " + diaryList.size());
                return diaryList;
    
            });
    
        }
    
        public void deleteAll() {
    
            entityStore.executeInTransaction(txn -> {
    
    
                txn.getAll(entityType).forEach(entity ->
                {
                    if (!entity.delete()) {
                        LOGGER.warn("cant delete {}", entityToDiary(entity, txn));
                    }
                });
            });
    
        }
    
    
        @PreDestroy
        public void atEnd() {
            entityStore.close();
        }
    
    }
    
    import jetbrains.exodus.ArrayByteIterable;
    import jetbrains.exodus.ByteIterable;
    import jetbrains.exodus.bindings.BindingUtils;
    import jetbrains.exodus.bindings.ComparableBinding;
    import jetbrains.exodus.util.LightOutputStream;
    import org.jetbrains.annotations.NotNull;
    
    import java.io.ByteArrayInputStream;
    import java.time.Instant;
    
        public class InstantBinding extends ComparableBinding {
    
        public static final InstantBinding BINDING = new InstantBinding();
    
        public static Instant entryToInstant(@NotNull final ByteIterable entry) {
            return (Instant) BINDING.entryToObject(entry);
        }
    
        public static ArrayByteIterable instantToEntry(final Instant object) {
            return BINDING.objectToEntry(object);
        }
    
        @Override
        public Comparable readObject(@NotNull ByteArrayInputStream stream) {
            final long l = BindingUtils.readLong(stream);
            final int i = BindingUtils.readInt(stream);
            return Instant.ofEpochSecond(l, i);
        }
    
        @Override
        public void writeObject(@NotNull LightOutputStream output, @NotNull Comparable object) {
            final Instant instant = (Instant) object;
            output.writeUnsignedLong(instant.getEpochSecond() ^ 0x8000000000000000L);
            output.writeUnsignedInt(instant.getNano() ^ 0x80000000);
    
        }
    
    }