Apache Ignite 和 Spring 数据 - 如何删除实体?

Apache Ignite and Spring Data - How to delete an entity?

我在持久存储模式下使用 Apache Ignite 和 Spring 数据(ignite 版本 2.9.1 和 spring 数据 2.2)。通过 Spring 数据存储库保存和读取对象工作正常。但是当我尝试删除一个实体时,尽管我尝试根据 the ignite spring data documentation.

使用 repo.deleteById() 方法,但它并没有从存储中删除

有什么提示吗?谢谢

备注 1: repo.deleteAll() 工作正常。

备注 2: DELETE FROM Item where Id = x 可以在 SQL 客户端上正常工作。

备注3:我在github上上传了例子https://github.com/egloffmark/ignite-test/tree/master/ignite-test-map

备注 4: 我注意到它似乎与从 repo find 方法获得的列表有关。看起来您不能通过同一个列表进行第二次迭代?以下似乎不起作用:

        // query items
        log.info("first iteration, reading items...");
        Iterable<Item> items = itemRepo.findAll();
        // first iteration
        for(Item item : items) {
            log.info(item.toString());
        }
    
        // second iteration on same result object  <--- NOT WORKING !!!!
        log.info("2nd iteration,...");
        for(Item item : items) {
            log.info(item.toString());
        }

输出(第一次执行)

Started MyIgniteMapApplication in 4.437 seconds (JVM running for 4.673)
XXXXXXXXXXX  application started... XXXXXXXXXXX
Number of Items: 0
storing items...
1st iteration, reading items...
Item(id=1, name=Advanced Xtra Item, code=AIX-1, attributes={Price=3.9, Color=red})
Item(id=2, name=Advanced Hyper Item, code=AIX-2, attributes={Price=9.9, Color=blue})
Item(id=3, name=Advanced Biggest Item, code=AIX-3, attributes={Price=8.95, Color=pink})
2nd iteration,...
deleting items...
Number of Items: 3
XXXXXXXXXXX  application finished... XXXXXXXXXXX

Classes

型号Class

@Data
public class Item {
    
    private static final AtomicLong ID_GEN = new AtomicLong();

    @QuerySqlField(index = true)
    private Long id;
    
    @QuerySqlField
    public String name;

    @QuerySqlField
    public String code;
    
    public Item(Long id, String code, String name) {
        this.id = id;
        this.code = code;
        this.name = name;
    }
    
    public Item(String code, String name) {
        this(ID_GEN.incrementAndGet(),code, name);
    }
}

存储库

@RepositoryConfig(cacheName = "ItemCache")
public interface ItemIgniteRepository extends IgniteRepository<Item, Long> {
}

Spring启动应用程序

@SpringBootApplication(scanBasePackages = { "com.ontius.ignite.config" })
@Slf4j
public class MyIgniteApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(MyIgniteApplication.class, args);
    }

    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @Autowired
    private ItemIgniteRepository itemRepo;

    @Override
    public void run(String... args) throws Exception {
        log.info("XXXXXXXXXXX  application started... XXXXXXXXXXX");

        long itemsCount = itemRepo.count();
        log.info("Number of Items: {}", itemsCount);
        if (itemsCount == 0) {
        
            // store some items
            Map<Long, Item> items = new TreeMap<Long,Item>();
            
            Item item = new Item("AIX-1","Advanced Xtra Item");
            items.put(item.getId(),item);
            
            item = new Item("AIX-2","Advanced Hyper Item");
            items.put(item.getId(),item);
    
            item = new Item("AIX-3","Advanced Ultra Item");
            items.put(item.getId(),item);
            log.info("storing items...");
            itemRepo.save(items);
        }
        
        // query items
        log.info("1st iteration, reading items...");
        Iterable<Item> items = itemRepo.findAll();
        // first iteration
        for(Item item : items) {
            log.info(item.toString());
        }
    
        // second iteration on same result object  <--- NOT WORKING !!!!
        log.info("2nd iteration,...");
        for(Item item : items) {
            log.info(item.toString());
        }
        
        // delete items <--- NOT WORKING !!!!
        log.info("deleting items...");
        for(Item item : items) {
            itemRepo.deleteById(item.getId());
        };
        itemsCount = itemRepo.count();
        log.info("Number of Items: {}", itemsCount);

        log.info("XXXXXXXXXXX  application finished... XXXXXXXXXXX");
        applicationContext.close();
    }
}

问题与“删除”或“点燃”本身无关。默认情况下,spring 数据 CrudRepository 是 returning 一个 Iterable<T>,根据规范只能使用一次。

public interface CrudRepository<T, ID> extends Repository<T, ID> {
 ...
    /**
     * Returns all instances of the type.
     *
     * @return all entities
     */
    Iterable<T> findAll();
}

此问题的一个常见解决方案是在您的 Repository 接口中定义自己的“findAll”方法,该方法 return 是列表类型,例如

@RepositoryConfig(cacheName = "ItemCache")
public interface ItemIgniteRepository extends IgniteRepository<Item, Long>, CustomItemIngniteRepository {
   ...  
    
    @Override
    public List<Item> findAll();
    
}

之后它按预期工作。您可以对同一个结果对象进行第二次迭代。

输出

XXXXXXXXXXX  application started... XXXXXXXXXXX
Number of Items: 0
storing items...
1st iteration, reading items...
Item(id=1, name=Advanced Xtra Item, code=AIX-1, attributes={Price=3.9, Color=red})
Item(id=2, name=Advanced Hyper Item, code=AIX-2, attributes={Price=9.9, Color=blue})
Item(id=3, name=Advanced Biggest Item, code=AIX-3, attributes={Price=8.95, Color=pink})
2nd iteration,...
Item(id=1, name=Advanced Xtra Item, code=AIX-1, attributes={Price=3.9, Color=red})
Item(id=2, name=Advanced Hyper Item, code=AIX-2, attributes={Price=9.9, Color=blue})
Item(id=3, name=Advanced Biggest Item, code=AIX-3, attributes={Price=8.95, Color=pink})
deleting items...
item: Item(id=1, name=Advanced Xtra Item, code=AIX-1, attributes={Price=3.9, Color=red})
item: Item(id=2, name=Advanced Hyper Item, code=AIX-2, attributes={Price=9.9, Color=blue})
item: Item(id=3, name=Advanced Biggest Item, code=AIX-3, attributes={Price=8.95, Color=pink})
Number of Items: 0
XXXXXXXXXX  application finished... XXXXXXXXXXX