这是我在casssandra中的用户定义类型:
create type app
( app_id uuid,
app_name varchar,
maker varchar
);
我的表格中包含此类User DefinedType的地图
create table device
( device_id timeuuid,
apps map<uuid,frozen<app>>,
primary key (device_id)
);
Java中的映射
@Table("device")
public class Device {
@PrimaryKey
@Column("device_id")
private UUID device_id;
@Column("apps")
private Map<UUID, App> apps;
}
和
@UserDefinedType
public class App {
@Column("app_id")
private UUID app_id;
@Column("app_name")
private String app_name;
@Column("maker")
private String maker;
}
现在使用标准的Sprind-data-cassandra CrudRepository:
public interface DeviceRepository extends CrudRepository<Device, UUID>{
当我试图保存到它时
Device st1 = new Device();
st1.setDevice_id(MyUtils.getRandomTimestampUUID());
/** Apps **/
App c1 = new App();
c1.setApp_id(MyUtils.getRandomTimestampUUID());
c1.setMaker("Maker of game 1");
c1.setApp_name("game 1");
App c2 = new App();
c2.setApp_id(MyUtils.getRandomTimestampUUID());
c2.setMaker("Maker of game 2");
c2.setApp_name("game 2");
Map<UUID, App> apps = new LinkedHashMap<UUID, App>();
apps.put(MyUtils.getRandomTimestampUUID(), c1);
apps.put(MyUtils.getRandomTimestampUUID(), c2);
st1.setApps(apps);
_deviceRepository.save(st1);
我得到了这个例外:
java.lang.IllegalArgumentException: Target bean of type java.util.LinkedHashMap is not of type of the persistent entity (com.pfellwock.cassandra.type.App)!
at org.springframework.util.Assert.isTrue(Assert.java:68) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.data.mapping.model.BasicPersistentEntity.getPropertyAccessor(BasicPersistentEntity.java:397) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter.getConvertingAccessor(MappingCassandraConverter.java:608) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter.write(MappingCassandraConverter.java:322) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter.getWriteValue(MappingCassandraConverter.java:713) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter.getWriteValue(MappingCassandraConverter.java:665) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter.access$000(MappingCassandraConverter.java:86) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter$2.doWithPersistentProperty(MappingCassandraConverter.java:340) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter$2.doWithPersistentProperty(MappingCassandraConverter.java:335) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:312) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter.writeInsertFromWrapper(MappingCassandraConverter.java:335) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter.writeInsertFromObject(MappingCassandraConverter.java:329) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter.write(MappingCassandraConverter.java:314) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.convert.MappingCassandraConverter.write(MappingCassandraConverter.java:298) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.core.CassandraTemplate.createInsertQuery(CassandraTemplate.java:948) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.core.CassandraTemplate.createInsertQuery(CassandraTemplate.java:717) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.core.CassandraTemplate.doInsert(CassandraTemplate.java:708) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:290) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
at org.springframework.data.cassandra.core.CassandraTemplate.insert(CassandraTemplate.java:285) ~[spring-data-cassandra-1.5.0.BUILD-SNAPSHOT.jar:na]
以下是gradle的设置:
compile("com.datastax.cassandra:cassandra-driver-core:3.1.1")
compile('org.springframework.boot:spring-boot-starter-data-cassandra')
compile("org.springframework.data:spring-data-cassandra:1.5.0.BUILD-SNAPSHOT")
compile("org.springframework.data:spring-cql:1.5.0.BUILD-SNAPSHOT")
答案 0 :(得分:1)
Currentlly spring数据不支持自定义数据类型作为地图“app”中的值。 即使你纠正了这个错误,你也会偶然发现其他类似的实体[Device]中类型[interface java.util.Map]的属性[app]的Collections中允许只有原始类型。
就此而言,错误将通过“@CassandraType(type = Name.MAP,userTypeName =”address“,typeArguments = {Name.TEXT,Name.CUSTOM})”
而消失。在类设备中的app属性上。 注意* map中的typeArguments仅支持原始类型,假设CUSTOM是假设的。