com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type text, expecting class ja va.lang.String but class com.example.scripts.TestEnumMapping$TestEnum provided

DataStax JIRA | Will Salisbury | 1 year ago
  1. 0

    I'm able to create a row with {{mapper.save(testEntity)}}, however, when I try and use the mapper to fetch a row I get the following exception: {noformat} 18:57:27.703 [main] DEBUG c.d.driver.mapping.EntityMapper - Preparing query INSERT INTO "test_keyspace"."test_table"("id","testenum") VA LUES (?,?); 18:57:27.723 [main] DEBUG c.d.driver.mapping.EntityMapper - Preparing query SELECT "id" AS col1,"testenum" AS col2 FROM "test_keyspace". "test_table" WHERE "id"=? AND "testenum"=?; Exception in thread "main" com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type text, expecting class ja va.lang.String but class com.example.scripts.TestEnumMapping$TestEnum provided at com.datastax.driver.core.DataType.serialize(DataType.java:619) at com.datastax.driver.mapping.Mapper.getQuery(Mapper.java:331) at com.datastax.driver.mapping.Mapper.getQuery(Mapper.java:316) at com.datastax.driver.mapping.Mapper.get(Mapper.java:361) at com.example.scripts.TestEnumMapping.main(TestEnumMapping.java:48) {noformat} This might be easier if I just paste an example app: {code} package com.example.scripts; import com.datastax.driver.core.Cluster; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ProtocolOptions; import com.datastax.driver.core.ProtocolVersion; import com.datastax.driver.core.Session; import com.datastax.driver.core.Statement; import com.datastax.driver.core.schemabuilder.SchemaBuilder; import com.datastax.driver.mapping.EnumType; import com.datastax.driver.mapping.Mapper; import com.datastax.driver.mapping.MappingManager; import com.datastax.driver.mapping.annotations.Enumerated; import com.datastax.driver.mapping.annotations.PartitionKey; import com.datastax.driver.mapping.annotations.Table; public class TestEnumMain { private static final String KEYSPACE = "test_keyspace"; private static final String TABLE = "test_table"; public static void main(String[] args) { Cluster cluster = Cluster.builder() .addContactPoints("127.0.0.1") .withProtocolVersion(ProtocolVersion.V3) .build(); Session session = cluster.connect(); MappingManager mappingManager = new MappingManager(session); Mapper<TestEntity> mapper = mappingManager.mapper(TestEntity.class); session.execute( String.format( "CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};", KEYSPACE ) ); Statement createTable = SchemaBuilder.createTable(KEYSPACE, TABLE) .addPartitionKey("id", DataType.text()) .addPartitionKey("testEnum", DataType.text()); session.execute(createTable); TestEntity testEntity = new TestEntity("foo", TestEnum.Some); mapper.save(testEntity); TestEntity testEntityResult = mapper.get("foo", TestEnum.Some); System.out.println(testEntityResult); } public enum TestEnum { Some, None } @Table(keyspace = KEYSPACE, name = TABLE) public static class TestEntity { @PartitionKey(0) private String id; @Enumerated(value = EnumType.STRING) @PartitionKey(1) private TestEnum testEnum; public TestEntity() { } public TestEntity(String id, TestEnum testEnum) { this.id = id; this.testEnum = testEnum; } public String getId() { return id; } public void setId(String id) { this.id = id; } public TestEnum getTestEnum() { return testEnum; } public void setTestEnum(TestEnum testEnum) { this.testEnum = testEnum; } @Override public String toString() { return String.format("<TestEntity id=%s testEnum=%s>", id, testEnum); } } } {code}

    DataStax JIRA | 1 year ago | Will Salisbury
    com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type text, expecting class ja va.lang.String but class com.example.scripts.TestEnumMapping$TestEnum provided
  2. 0

    I'm able to create a row with {{mapper.save(testEntity)}}, however, when I try and use the mapper to fetch a row I get the following exception: {noformat} 18:57:27.703 [main] DEBUG c.d.driver.mapping.EntityMapper - Preparing query INSERT INTO "test_keyspace"."test_table"("id","testenum") VA LUES (?,?); 18:57:27.723 [main] DEBUG c.d.driver.mapping.EntityMapper - Preparing query SELECT "id" AS col1,"testenum" AS col2 FROM "test_keyspace". "test_table" WHERE "id"=? AND "testenum"=?; Exception in thread "main" com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type text, expecting class ja va.lang.String but class com.example.scripts.TestEnumMapping$TestEnum provided at com.datastax.driver.core.DataType.serialize(DataType.java:619) at com.datastax.driver.mapping.Mapper.getQuery(Mapper.java:331) at com.datastax.driver.mapping.Mapper.getQuery(Mapper.java:316) at com.datastax.driver.mapping.Mapper.get(Mapper.java:361) at com.example.scripts.TestEnumMapping.main(TestEnumMapping.java:48) {noformat} This might be easier if I just paste an example app: {code} package com.example.scripts; import com.datastax.driver.core.Cluster; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ProtocolOptions; import com.datastax.driver.core.ProtocolVersion; import com.datastax.driver.core.Session; import com.datastax.driver.core.Statement; import com.datastax.driver.core.schemabuilder.SchemaBuilder; import com.datastax.driver.mapping.EnumType; import com.datastax.driver.mapping.Mapper; import com.datastax.driver.mapping.MappingManager; import com.datastax.driver.mapping.annotations.Enumerated; import com.datastax.driver.mapping.annotations.PartitionKey; import com.datastax.driver.mapping.annotations.Table; public class TestEnumMain { private static final String KEYSPACE = "test_keyspace"; private static final String TABLE = "test_table"; public static void main(String[] args) { Cluster cluster = Cluster.builder() .addContactPoints("127.0.0.1") .withProtocolVersion(ProtocolVersion.V3) .build(); Session session = cluster.connect(); MappingManager mappingManager = new MappingManager(session); Mapper<TestEntity> mapper = mappingManager.mapper(TestEntity.class); session.execute( String.format( "CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};", KEYSPACE ) ); Statement createTable = SchemaBuilder.createTable(KEYSPACE, TABLE) .addPartitionKey("id", DataType.text()) .addPartitionKey("testEnum", DataType.text()); session.execute(createTable); TestEntity testEntity = new TestEntity("foo", TestEnum.Some); mapper.save(testEntity); TestEntity testEntityResult = mapper.get("foo", TestEnum.Some); System.out.println(testEntityResult); } public enum TestEnum { Some, None } @Table(keyspace = KEYSPACE, name = TABLE) public static class TestEntity { @PartitionKey(0) private String id; @Enumerated(value = EnumType.STRING) @PartitionKey(1) private TestEnum testEnum; public TestEntity() { } public TestEntity(String id, TestEnum testEnum) { this.id = id; this.testEnum = testEnum; } public String getId() { return id; } public void setId(String id) { this.id = id; } public TestEnum getTestEnum() { return testEnum; } public void setTestEnum(TestEnum testEnum) { this.testEnum = testEnum; } @Override public String toString() { return String.format("<TestEntity id=%s testEnum=%s>", id, testEnum); } } } {code}

    DataStax JIRA | 1 year ago | Will Salisbury
    com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type text, expecting class ja va.lang.String but class com.example.scripts.TestEnumMapping$TestEnum provided
  3. 0

    Can't retrieve by UDT key from cassandra with datastax java driver

    Stack Overflow | 1 year ago | NullProgrammerException
    com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type frozen<example_keyspace.my_data_type>, expecting class com.datastax.driver.core.UDTValue but class me.example.Main$MyDataType provided
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Reported on [Stack Overflow|http://stackoverflow.com/questions/31373008/cant-retrieve-by-udt-key-from-cassandra-with-datastax-java-driver] Schema: {code} create keyspace example_keyspace; use example_keyspace; create type my_data_type (value text); create table my_classes (key frozen<my_data_type>, value frozen<my_data_type>, primary key (key)); {code} Java code: {code} package me.example; import com.datastax.driver.core.Cluster; import com.datastax.driver.mapping.Mapper; import com.datastax.driver.mapping.MappingManager; import com.datastax.driver.mapping.annotations.Frozen; import com.datastax.driver.mapping.annotations.PartitionKey; import com.datastax.driver.mapping.annotations.Table; import com.datastax.driver.mapping.annotations.UDT; public class Main { public static void main(String[] args) { try (Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1") .build()) { Mapper<MyClass> mapper = new MappingManager(cluster.newSession()) .mapper(MyClass.class); MyDataType value = new MyDataType(); value.setValue("theValue"); MyDataType key = new MyDataType(); key.setValue("theKey"); MyClass myClass = new MyClass(); myClass.setKey(key); myClass.setValue(value); mapper.save(myClass); MyClass toret = mapper.get(key); System.out.println(toret.getKey()); System.out.println(toret.getValue().getValue()); } } @Table(keyspace = "example_keyspace", name = "my_classes") public static class MyClass { @PartitionKey @Frozen private MyDataType key; @Frozen private MyDataType value; public MyDataType getKey() { return key; } public void setKey(MyDataType key) { this.key = key; } public MyDataType getValue() { return value; } public void setValue(MyDataType value) { this.value = value; } } @UDT(keyspace = "example_keyspace", name = "my_data_type") public static class MyDataType { private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((value == null) ? 0 : value.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof MyDataType)) { return false; } MyDataType other = (MyDataType) obj; if (value == null) { if (other.value != null) { return false; } } else if (!value.equals(other.value)) { return false; } return true; } } } {code} It fails with this exception: {code} Exception in thread "main" com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type frozen<example_keyspace.my_data_type>, expecting class com.datastax.driver.core.UDTValue but class me.example.Main$MyDataType provided at com.datastax.driver.core.DataType.serialize(DataType.java:619) at com.datastax.driver.mapping.Mapper.getQuery(Mapper.java:320) at com.datastax.driver.mapping.Mapper.get(Mapper.java:342) at me.example.Main.main(Main.java:31) {code} There is a workaround: {code} MappingManager manager = new MappingManager(cluster.newSession()); UDTMapper<MyDataType> myDataTypeMapper = manager.udtMapper(MyDataType.class); UDTValue keyAsUDTValue = myDataTypeMapper.toUDT(key); MyClass toret = mapper.get(keyAsUDTValue); {code}

    DataStax JIRA | 1 year ago | Olivier Michallat
    com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type frozen<example_keyspace.my_data_type>, expecting class com.datastax.driver.core.UDTValue but class me.example.Main$MyDataType provided
  6. 0

    Reported on [Stack Overflow|http://stackoverflow.com/questions/31373008/cant-retrieve-by-udt-key-from-cassandra-with-datastax-java-driver] Schema: {code} create keyspace example_keyspace; use example_keyspace; create type my_data_type (value text); create table my_classes (key frozen<my_data_type>, value frozen<my_data_type>, primary key (key)); {code} Java code: {code} package me.example; import com.datastax.driver.core.Cluster; import com.datastax.driver.mapping.Mapper; import com.datastax.driver.mapping.MappingManager; import com.datastax.driver.mapping.annotations.Frozen; import com.datastax.driver.mapping.annotations.PartitionKey; import com.datastax.driver.mapping.annotations.Table; import com.datastax.driver.mapping.annotations.UDT; public class Main { public static void main(String[] args) { try (Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1") .build()) { Mapper<MyClass> mapper = new MappingManager(cluster.newSession()) .mapper(MyClass.class); MyDataType value = new MyDataType(); value.setValue("theValue"); MyDataType key = new MyDataType(); key.setValue("theKey"); MyClass myClass = new MyClass(); myClass.setKey(key); myClass.setValue(value); mapper.save(myClass); MyClass toret = mapper.get(key); System.out.println(toret.getKey()); System.out.println(toret.getValue().getValue()); } } @Table(keyspace = "example_keyspace", name = "my_classes") public static class MyClass { @PartitionKey @Frozen private MyDataType key; @Frozen private MyDataType value; public MyDataType getKey() { return key; } public void setKey(MyDataType key) { this.key = key; } public MyDataType getValue() { return value; } public void setValue(MyDataType value) { this.value = value; } } @UDT(keyspace = "example_keyspace", name = "my_data_type") public static class MyDataType { private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((value == null) ? 0 : value.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof MyDataType)) { return false; } MyDataType other = (MyDataType) obj; if (value == null) { if (other.value != null) { return false; } } else if (!value.equals(other.value)) { return false; } return true; } } } {code} It fails with this exception: {code} Exception in thread "main" com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type frozen<example_keyspace.my_data_type>, expecting class com.datastax.driver.core.UDTValue but class me.example.Main$MyDataType provided at com.datastax.driver.core.DataType.serialize(DataType.java:619) at com.datastax.driver.mapping.Mapper.getQuery(Mapper.java:320) at com.datastax.driver.mapping.Mapper.get(Mapper.java:342) at me.example.Main.main(Main.java:31) {code} There is a workaround: {code} MappingManager manager = new MappingManager(cluster.newSession()); UDTMapper<MyDataType> myDataTypeMapper = manager.udtMapper(MyDataType.class); UDTValue keyAsUDTValue = myDataTypeMapper.toUDT(key); MyClass toret = mapper.get(keyAsUDTValue); {code}

    DataStax JIRA | 1 year ago | Olivier Michallat
    com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type frozen<example_keyspace.my_data_type>, expecting class com.datastax.driver.core.UDTValue but class me.example.Main$MyDataType provided

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. com.datastax.driver.core.exceptions.InvalidTypeException

      Invalid value for CQL type text, expecting class ja va.lang.String but class com.example.scripts.TestEnumMapping$TestEnum provided

      at com.datastax.driver.core.DataType.serialize()
    2. DataStax Java Driver for Apache Cassandra - Core
      DataType.serialize
      1. com.datastax.driver.core.DataType.serialize(DataType.java:619)
      1 frame
    3. com.datastax.driver
      Mapper.get
      1. com.datastax.driver.mapping.Mapper.getQuery(Mapper.java:331)
      2. com.datastax.driver.mapping.Mapper.getQuery(Mapper.java:316)
      3. com.datastax.driver.mapping.Mapper.get(Mapper.java:361)
      3 frames
    4. com.example.scripts
      TestEnumMapping.main
      1. com.example.scripts.TestEnumMapping.main(TestEnumMapping.java:48)
      1 frame