spring-data-cassandra找不到用户定义tytype



基于教程spring-data-cassandra-Tutorial,我正在尝试使用UserDefinedType注释。

我升级了使用Spring-Data-Cassandra的项目:1.5.0.Build-SnapShot,将属性价格添加到本书中,实现了相应的UserDefinedType并添加了SetInitialentitySet和SetUsertyPeryPeresolver。

然后,当我针对Cassandra 3.9执行BookRepositoryintegrationTest时,我会收到以下错误:

org.springframework.data.mapping.model.mappingexception:用户类型[价格]找不到

知道我缺少什么吗?

在这里我的更改...

book.java:

package org.baeldung.spring.data.cassandra.model;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.springframework.cassandra.core.Ordering;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;
@Table
public class Book {
    @PrimaryKeyColumn(name = "id", ordinal = 0, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
    private UUID id;
    @PrimaryKeyColumn(name = "title", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
    private String title;
    @PrimaryKeyColumn(name = "publisher", ordinal = 2, type = PrimaryKeyType.PARTITIONED)
    private String publisher;
    @Column
    private Set<String> tags = new HashSet<>();
    private Price price;
    public Book(final UUID id, final String title, final String publisher, final Set<String> tags) {
        this.id = id;
        this.title = title;
        this.publisher = publisher;
        this.tags.addAll(tags);
    }
}

pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.baeldung</groupId>
    <artifactId>spring-data-cassandra</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>spring-data-cassandra</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>4.3.4.RELEASE</org.springframework.version>
        <org.springframework.data.version>1.5.0.BUILD-SNAPSHOT</org.springframework.data.version>
        <junit.version>4.12</junit.version>
        <org.slf4j.version>1.7.12</org.slf4j.version>
        <logback.version>1.1.3</logback.version>
        <cassandra-driver-core.version>3.1.2</cassandra-driver-core.version>
        <cassandra-unit.version>3.0.0.1</cassandra-unit.version>
        <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>        
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-cassandra</artifactId>
            <version>${org.springframework.data.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.cassandraunit</groupId>
            <artifactId>cassandra-unit-spring</artifactId>
            <version>${cassandra-unit.version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.cassandraunit</groupId>
                    <artifactId>cassandra-unit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.cassandraunit</groupId>
            <artifactId>cassandra-unit</artifactId>
            <version>${cassandra-unit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-core</artifactId>
            <version>${cassandra-driver-core.version}</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>${org.slf4j.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
                     <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${maven-surefire-plugin.version}</version>
                        <configuration>
                            <excludes>
                                <exclude>**/*IntegrationTest.java</exclude>
                                <exclude>**/*LiveTest.java</exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
    <profiles>
        <profile>
            <id>integration</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>test</goal>
                                </goals>
                                <configuration>
                                    <excludes>
                                        <exclude>**/*LiveTest.java</exclude>
                                    </excludes>
                                    <includes>
                                        <include>**/*IntegrationTest.java</include>
                                    </includes>
                                </configuration>
                            </execution>
                        </executions>
                        <configuration>
                            <systemPropertyVariables>
                                <test.mime>json</test.mime>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

cassandraconfig.java

package org.baeldung.spring.data.cassandra.config;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.baeldung.spring.data.cassandra.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.CassandraEntityClassScanner;
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.mapping.SimpleUserTypeResolver;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
@EnableCassandraRepositories(basePackages = "org.baeldung.spring.data.cassandra.repository")
public class CassandraConfig extends AbstractCassandraConfiguration {
    private static final Log LOGGER = LogFactory.getLog(CassandraConfig.class);
    @Autowired
    private Environment environment;
    @Override
    protected String getKeyspaceName() {
        return environment.getProperty("cassandra.keyspace");
    }
    @Override
    @Bean
    public CassandraClusterFactoryBean cluster() {
        final CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
        cluster.setContactPoints(environment.getProperty("cassandra.contactpoints"));
        cluster.setPort(Integer.parseInt(environment.getProperty("cassandra.port")));
        LOGGER.info("Cluster created with contact points [" + environment.getProperty("cassandra.contactpoints") + "] " + "& port [" + Integer.parseInt(environment.getProperty("cassandra.port")) + "].");
        return cluster;
    }
    @Override
    @Bean
    public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
        BasicCassandraMappingContext ctx = new BasicCassandraMappingContext();
        ctx.setInitialEntitySet(CassandraEntityClassScanner.scan(Book.class));
        ctx.setUserTypeResolver(new SimpleUserTypeResolver(cluster().getObject(), getKeyspaceName()));
        return ctx;
    }
}

price.java:

package org.baeldung.spring.data.cassandra.model;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.UserDefinedType;
@UserDefinedType
public class Price {
    @Column
    private Double amount;
    @Column
    private String currency;
    public Price(Double amount, String currency) {
        this.amount = amount;
        this.currency = currency;
    }
    public Double getAmount() {
        return amount;
    }
    public String getCurrency() {
        return currency;
    }
}

尝试使用下面的cassandraconfig类。

@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
@EnableCassandraRepositories(basePackages = "org.baeldung.spring.data.cassandra.repository")
public class CassandraConfig extends AbstractCassandraConfiguration {
private static final Log LOGGER = LogFactory.getLog(CassandraConfig.class);
@Autowired
private Environment environment;
@Override
protected String getKeyspaceName() {
    return environment.getProperty("cassandra.keyspace");
}
@Override
@Bean
public CassandraClusterFactoryBean cluster() {
    final CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
    cluster.setContactPoints(environment.getProperty("cassandra.contactpoints"));
    cluster.setPort(Integer.parseInt(environment.getProperty("cassandra.port")));
    LOGGER.info("Cluster created with contact points [" + environment.getProperty("cassandra.contactpoints") + "] " + "& port [" + Integer.parseInt(environment.getProperty("cassandra.port")) + "].");
    return cluster;
}

@Bean
public CassandraMappingContext mappingContext() throws ClassNotFoundException {
    BasicCassandraMappingContext mappingContext = new BasicCassandraMappingContext();
    mappingContext.setInitialEntitySet(CassandraEntityClassScanner.scan("org.baeldung.spring.data.cassandra.model"));
    mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster().getObject(),getKeyspaceName()));
    return mappingContext;
}
@Bean
public CassandraConverter converter() throws ClassNotFoundException {
    return new MappingCassandraConverter(mappingContext());
}
@Bean
public CassandraSessionFactoryBean session() throws ClassNotFoundException {
    CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
    session.setCluster(cluster().getObject());
    session.setKeyspaceName(getKeyspaceName());
    session.setConverter(converter());
    session.setSchemaAction(SchemaAction.RECREATE);
    return session;
  }
}

您的代码看起来不错,但错过了用户类型的创建。您还配置了查找用户类型所需的SimpleUserTypeResolver

用户类型[价格]找不到表示在Cassandra中找不到用户类型。

您有两个选择方法:

  1. 您自己创建用户定义的类型。
  2. 在引导应用程序时使用SchemaAction.RECREATE

Baeldung的教程用户表创建在测试中,因此在表创建之前手动用户类型创建可能是更好的选择。

CreateUserTypeSpecification userTypeSpecification = mappingContext.
                             getCreateUserTypeSpecificationFor(mappingContext.getExistingPersistentEntity(Price.class))
                            .ifNotExists(ifNotExists);
cqlTemplate.execute(CreateUserTypeCqlGenerator.toCql(userTypeSpecification));

相关内容

  • 没有找到相关文章

最新更新