【发布时间】:2019-09-04 01:02:04
【问题描述】:
每次我尝试交替使用 Ignite Spring API 和 Ignite SQL API 时,都会出现类转换异常或“找不到 xxxx 的 sql 表”
发生这种情况是因为我使用 ignite spring 创建了一个缓存并尝试使用 Ignite SQL API 使用数据,反之亦然:
@Configuration
@EnableIgniteRepositories("local.teste.is.api.repositories")
public class SpringAppCfg {
@Bean
public Ignite igniteInstance() {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setIgniteInstanceName("springDataNode");
cfg.setPeerClassLoadingEnabled(true);
cfg.setClientMode(true);
...
// Defining and creating a new cache to be used by Ignite Spring Data
// repository.
CacheConfiguration ccfg = new CacheConfiguration("SQL_PUBLIC_SAMPLETYPE3");
// Setting SQL schema for the cache.
ccfg.setIndexedTypes(Integer.class, SampleType.class);
cfg.setCacheConfiguration(ccfg);
Ignite ignite = Ignition.start(cfg);
IgniteCache cache = ignite.getOrCreateCache(ccfg);
SqlQuery sql = new SqlQuery(SampleType.class, "true");
try (QueryCursor<Entry<Integer, SampleType>> cursor = cache.query(sql)) {
for (Entry<Integer, SampleType> e : cursor)
System.out.println(e.getValue().toString());
}
return ignite;
}
SampleTypeRepository:
package local.teste.is.api.repositories;
import org.apache.ignite.springdata.repository.IgniteRepository;
import org.apache.ignite.springdata.repository.config.RepositoryConfig;
import local.al40.is.api.entities.SampleType;
@RepositoryConfig(cacheName = "SQL_PUBLIC_SAMPLETYPE")
public interface SampleTypeRepository extends IgniteRepository<SampleType, Integer> {
public SampleType getSampleTypeBySampleTypeId(Integer id);
}
使用 ignite Spring 的一切工作,包括保存和读取数据:
public class Application {
private static AnnotationConfigApplicationContext dataCtx;
private static SampleTypeRepository repo;
public static void main(String[] args) throws Exception {
dataCtx = new AnnotationConfigApplicationContext();
// Explicitly registering Spring configuration.
dataCtx.register(SpringAppCfg.class);
dataCtx.refresh();
repo = dataCtx.getBean(SampleTypeRepository.class);
System.out.println(repo);
SampleType s = new SampleType(1, "teste");
repo.save(s.getSampleTypeId(), s);
System.out.println(repo.getSampleTypeBySampleTypeId(1).getSampleTypeName());
如果我通过 DDL 创建一个 Ignite SQL 表并尝试通过 Ignite Spring 使用它,就会发生这种情况,给我一个 ClassCastException,例如“...ignite.IgniteRepositoryImpl#123456 cannot be cast to SampleType.class”。这使我相信这是与序列化有关的问题。如果我通过 Ignite Spring 创建表并尝试查询它,它还会给我“找不到 xxxx 的 sql 表”。有人尝试过整合这两种观点吗?我在网上找到的示例仅假设使用 Ignite Spring 创建的缓存,并且 Ignite 的文档表明这种交换是可能的并且是透明的。但是,显然不是,除非我忘记了什么。
最好的问候,
卡洛斯·科斯塔
【问题讨论】:
-
你能说明
SampleTypeRepository类型是如何定义的吗?