问题
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
Thanks
回答1:
See the details here: https://jira.spring.io/browse/DATACASS-172
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
> Invocation of init method failed; nested exception is org.springframework.data.cassandra.mapping.VerifierMappingExceptions: Cassandra entities must have the @Table, @Persistent or @PrimaryKeyClass Annotation
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;
}
}
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);
}
I've tested this solution and it's works 100%. In addition I've attached my POJO objects:
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 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)
At first glance - it's uncomfortable, but it allows you to work with objects that includes UDT and that don't.
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 {
..........
}
回答2:
User Defined data type is now supported by Spring Data Cassandra. The latest release 1.5.0.RELEASE uses Cassandra Data stax driver 3.1.3 and hence its working now. Follow the below steps to make it working
How to use UserDefinedType(UDT) feature with Spring Data Cassandra :
We need to use the latest jar of Spring data Cassandra (1.5.0.RELEASE)
group: 'org.springframework.data', name: 'spring-data-cassandra', version: '1.5.0.RELEASE'
Make sure it uses below versions of the jar :
datastax.cassandra.driver.version=3.1.3 spring.data.cassandra.version=1.5.0.RELEASE spring.data.commons.version=1.13.0.RELEASE spring.cql.version=1.5.0.RELEASE
Create user defined type in Cassandra : The type name should be same as defined in the POJO class
Address data type
CREATE TYPE address_type (
id text,
address_type text,
first_name text,
phone text
);
Create column-family with one of the columns as UDT in Cassandra:
Employee table:
CREATE TABLE employee( employee_id uuid, employee_name text, address frozen, primary key (employee_id, employee_name) );
In the domain class, define the field with annotation -CassandraType and DataType should be UDT:
@Table("employee") public class Employee { -- othere fields-- @CassandraType(type = DataType.Name.UDT, userTypeName = "address_type") private Address address; }
Create domain class for the user defined type : We need to make sure that column name in the user defined type schema has to be same as field name in the domain class.
@UserDefinedType("address_type") public class Address { @CassandraType(type = DataType.Name.TEXT) private String id; @CassandraType(type = DataType.Name.TEXT) private String address_type; }
In the Cassandra Config, Change this :
@Bean public CassandraMappingContext mappingContext() throws Exception { BasicCassandraMappingContext mappingContext = new BasicCassandraMappingContext(); mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster().getObject(), cassandraKeyspace)); return mappingContext; }
User defined type should have the same name across everywhere. for e.g
- @UserDefinedType("address_type")
- @CassandraType(type = DataType.Name.UDT, userTypeName = "address_type")
- CREATE TYPE address_type
来源:https://stackoverflow.com/questions/34753799/does-spring-data-cassandra-1-3-2-release-support-udt-annotations