Spring-data-Cassandra 1.3.2.RELEASE是否支持UDT注释? [英] Does Spring-data-Cassandra 1.3.2.RELEASE support UDT annotations?

查看:98
本文介绍了Spring-data-Cassandra 1.3.2.RELEASE是否支持UDT注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是@UDT( http: // Spring-data-Cassandra 1.3.2支持的//docs.datastax.com/zh-CN/developer/java-driver/2.1/java-driver/reference/mappingUdts.html )。如果没有,我如何为此添加解决方案

Is @UDT (http://docs.datastax.com/en/developer/java-driver/2.1/java-driver/reference/mappingUdts.html) supported by Spring-data-Cassandra 1.3.2.RELEASE? If not, how can I add workaround for this

谢谢

推荐答案

在此处查看详细信息:
https://jira.spring.io/browse/DATACASS -172

See the details here: https://jira.spring.io/browse/DATACASS-172

我遇到了同样的问题,听起来好像没有(
调试过程向我表明spring数据cassandra检查了
@ Table,@ Persistent或@PrimaryKeyClass仅批注,在其他情况下引发异常

I faced with the same issue and it sounds like it does not( debug process shows me that spring data cassandra check for @Table, @Persistent or @PrimaryKeyClass Annotation only and raise exception in other case


>
调用init方法失败;嵌套异常为org.springframework.data.cassandra.mapping.VerifierMappingExceptions:
Cassandra实体必须具有@ Table,@ Persistent或@PrimaryKeyClass批注

> Invocation of init method failed; nested exception is org.springframework.data.cassandra.mapping.VerifierMappingExceptions: Cassandra entities must have the @Table, @Persistent or @PrimaryKeyClass Annotation

但是我找到了解决方案。
我想出了一种方法,可以管理包含UDT和不包含UDT的实体。在我的应用程序中,我使用spring cassandra数据项目与使用直接的datastax核心驱动程序。不包含UDT对象的存储库使用spring cassanta数据方法,包含UDT的对象使用自定义存储库。
自定义存储库使用datastax映射器,并且可以与UDT
正常工作(它们位于单独的程序包中,请参阅下面的注释,为什么需要它):

But I found the solution. I figured out the approach that allows me to manage entities that include UDT and the ones that don't. In my application I use spring cassandra data project together with using of direct datastax core driver. The repositories that don't contain object with UDT use spring cassanta data approach and the objects that include UDT use custom repositories. Custom repositories use datastax mapper and they work correctly with UDT (they located in separate package, see notes below why it's needed):

package com.fyb.cassandra.custom.repositories.impl;

import java.util.List;
import java.util.UUID;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
import com.datastax.driver.mapping.Result;
import com.google.common.collect.Lists;
import com.fyb.cassandra.custom.repositories.AccountDeviceRepository;
import com.fyb.cassandra.dto.AccountDevice;

public class AccountDeviceRepositoryImpl implements AccountDeviceRepository {

    @Autowired
    public CassandraSessionFactoryBean session;

    private Mapper<AccountDevice> mapper;

    @PostConstruct
    void initialize() {
        mapper = new MappingManager(session.getObject()).mapper(AccountDevice.class);
    }

    @Override
    public List<AccountDevice> findAll() {
        return fetchByQuery("SELECT * FROM account_devices");
    }

    @Override
    public void save(AccountDevice accountDevice) {
        mapper.save(accountDevice);
    }

    @Override
    public void deleteByConditions(UUID accountId, UUID systemId, UUID deviceId) {
        final String query = "DELETE FROM account_devices where account_id =" + accountId + " AND system_id=" + systemId
                + " AND device_id=" + deviceId;
        session.getObject().execute(query);
    }

    @Override
    public List<AccountDevice> findByAccountId(UUID accountId) {
        final String query = "SELECT * FROM account_devices where account_id=" + accountId;
        return fetchByQuery(query);
    }

    /*
     * Take any valid CQL query and try to map result set to the given list of appropriates <T> types.
     */
    private List<AccountDevice> fetchByQuery(String query) {
        ResultSet results = session.getObject().execute(query);
        Result<AccountDevice> accountsDevices = mapper.map(results);
        List<AccountDevice> result = Lists.newArrayList();
        for (AccountDevice accountsDevice : accountsDevices) {
            result.add(accountsDevice);
        }
        return result;
    }
}

与管理实体合理的与春季数据相关的存储库不包含UDT对象的对象如下所示:

And the spring data related repos that resonsible for managing entities that don't include UDT objects looks like as follows:

package com.fyb.cassandra.repositories;

import org.springframework.data.cassandra.repository.CassandraRepository;

import com.fyb.cassandra.dto.AccountUser;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public interface AccountUserRepository extends CassandraRepository<AccountUser> {

    @Query("SELECT * FROM account_users WHERE account_id=?0")
    List<AccountUser> findByAccountId(UUID accountId);
}

我已经测试了此解决方案,并且可以100%使用。
此外,我还附加了POJO对象:

I've tested this solution and it's works 100%. In addition I've attached my POJO objects:

仅使用数据stax修饰的Pojo:

Pojo that uses only data stax annatation:

package com.fyb.cassandra.dto;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.datastax.driver.mapping.annotations.ClusteringColumn;
import com.datastax.driver.mapping.annotations.Column;
import com.datastax.driver.mapping.annotations.Frozen;
import com.datastax.driver.mapping.annotations.FrozenValue;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;

@Table(name = "account_systems")
public class AccountSystem {

    @PartitionKey
    @Column(name = "account_id")
    private java.util.UUID accountId;

    @ClusteringColumn
    @Column(name = "system_id")
    private java.util.UUID systemId;

    @Frozen
    private Location location;

    @FrozenValue
    @Column(name = "user_token")
    private List<UserToken> userToken;

    @Column(name = "product_type_id")
    private int productTypeId;

    @Column(name = "serial_number")
    private String serialNumber;   
}

Pojo不使用UDT,仅使用spring data cassandra框架:

Pojo without using UDT and using only spring data cassandra framework:

package com.fyb.cassandra.dto;

import java.util.Date;
import java.util.UUID;

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(value = "accounts")
public class Account {

    @PrimaryKeyColumn(name = "account_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    private java.util.UUID accountId;

    @Column(value = "account_name")
    private String accountName;

    @Column(value = "currency")
    private String currency;    
}

请注意,以下实体使用不同的注释:

Note, that the entities below use different annotations:

@PrimaryKeyColumn(name = "account_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)and @PartitionKey
@ClusteringColumn and @PrimaryKeyColumn(name = "area_parent_id", ordinal = 2, type = PrimaryKeyType.CLUSTERED)

乍一看-是令人不舒服,但是它允许您使用包含UDT且不包含UDT的对象。

At first glance - it's uncomfortable, but it allows you to work with objects that includes UDT and that don't.

一个重要说明。这两个存储库(使用UDT且不应该驻留在不同的程序包中)导致Spring配置寻找具有存储库的基本程序包:

One important note. That two repos(that use UDT and don't should reside in different packages) cause Spring config looking for base packages with repos:

@Configuration
@EnableCassandraRepositories(basePackages = {
        "com.fyb.cassandra.repositories" })
public class CassandraConfig {
..........
}

这篇关于Spring-data-Cassandra 1.3.2.RELEASE是否支持UDT注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆