spring数据cassandra找不到实体类

aelbi1ox  于 2021-06-14  发布在  Cassandra
关注(0)|答案(0)|浏览(361)

我试图保持一个cassandra实体,但在启动时我得到: Caused by: org.springframework.data.mapping.MappingException: Couldn't find PersistentEntity for type class com.service.model.Cart! 这是我的实体类:

package com.service.model;

import io.vavr.collection.LinkedHashMap;
import io.vavr.collection.Map;
import lombok.Getter;
import lombok.ToString;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;

@ToString
@Table("carts")
public class Cart {

  @Getter
  @PrimaryKey
  private final UUID uuid;
  private final Map<CartItemKey, CartItem> items;

  public Cart(UUID uuid) {
    this(uuid, LinkedHashMap.empty());
  }

  private Cart(UUID uuid, Map<CartItemKey, CartItem> items) {
    this.uuid = Objects.requireNonNull(uuid, "Cart's uuid cannot be null");
    this.items = Objects.requireNonNull(items, "Cart's items cannot be null");
  }
}

这是我的cassandraconfig:

package com.service.configuration;

import com.service.model.Cart;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.cassandra.config.AbstractClusterConfiguration;
import org.springframework.data.cassandra.core.convert.CassandraCustomConversions;
import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.util.Collections.singletonList;

@Configuration
public class CassandraConfig extends AbstractClusterConfiguration {

  @Value("${spring.data.cassandra.keyspace-name}")
  private String keyspaceName;
  @Value("${spring.data.cassandra.contact-points}")
  private String contactPoints;

  @Override
  protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
    return singletonList(
        CreateKeyspaceSpecification.createKeyspace(keyspaceName)
        .ifNotExists()
        .with(KeyspaceOption.DURABLE_WRITES, true)
        .withSimpleReplication());
  }

  @Override
  protected boolean getMetricsEnabled() {
    return false;
  }

  @Override
  protected String getContactPoints() {
    return contactPoints;
  }

  @Bean
  public CassandraCustomConversions customConversions() {
    List<Converter<?, ?>> converters = new ArrayList<>();
    converters.add(new CartWriteConverter());
    converters.add(new CartReadConverter());
    return new CassandraCustomConversions(converters);
  }

  static class CartWriteConverter implements Converter<Cart, String> {
    public String convert(Cart source) {
      try {
        return new ObjectMapper().writeValueAsString(source);
      } catch (IOException e) {
        throw new IllegalStateException(e);
      }
    }
  }

  static class CartReadConverter implements Converter<String, Cart> {
    public Cart convert(String source) {
      if (StringUtils.hasText(source)) {
        try {
          return new ObjectMapper().readValue(source, Cart.class);
        } catch (IOException e) {
          throw new IllegalStateException(e);
        }
      }
      return null;
    }
  }
}

最后是我的申请:

package com.service.cart;

import org.axonframework.springboot.autoconfig.AxonServerAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableCaching
@EnableAsync
@EnableFeignClients
@SpringBootApplication
@EnableAutoConfiguration(exclude = {
    DataSourceAutoConfiguration.class,
    DataSourceTransactionManagerAutoConfiguration.class,
    HibernateJpaAutoConfiguration.class,
    AxonServerAutoConfiguration.class})
@EnableCassandraRepositories(basePackages = "com.service.repository")
public class Application extends SpringBootServletInitializer {

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

令我不解的是 customConversions() bean它失败了,出现了另一个错误——无法MapvavrMap,所以spring必须扫描并注册这个实体,以便对它进行检查。这是预期的,虽然它不是Cassandra数据类型,但在我的理解添加我的自定义转换应该可以解决这个问题。
我也尝试过 @EntityScan 同样的结果。
任何帮助都将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题