Sewook Wee
Sewook Wee

Reputation: 277

Spring-data-cassandra's CassandraTemplate returns String, not a specified Object, when run queryForObject function.

I've been going through the Spring Data Cassandra documentation (http://docs.spring.io/spring-data/cassandra/docs/1.0.1.RELEASE/reference/html/cassandra.core.html) Basically, with proper annotation, I hoped the CassandraTemplate maps a row to a POJO object, but it didn't work as I expected.

For the call,

cassandraOps.queryForObject(s, Person.class)

I received an error as following:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to Person

Anything that I'm missing? Following is the same copy and paste from the doc above.

Person Class looks like:

@Table
public class Person {

    @PrimaryKey
    private String id;

    private String name;
    private int age;

    public Person(String id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {    
        return age;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
    }

}

and the application class looks like...:

public class CassandraApp {
    private static final Logger LOG = LoggerFactory.getLogger(CassandraApp.class);

    private static Cluster cluster;
    private static Session session;

    public static void main(String[] args) {

        try {

            cluster = Cluster.builder().addContactPoints(InetAddress.getLocalHost()).build();
            session = cluster.connect("mykeyspace");
            CassandraOperations cassandraOps = new CassandraTemplate(session);
            cassandraOps.insert(new Person("1234567890", "David", 40));

            Select s = QueryBuilder.select().from("person");
            s.where(QueryBuilder.eq("id", "1234567890"));

            LOG.info(cassandraOps.queryForObject(s, Person.class).getId());

            cassandraOps.truncate("person");

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
   }
}

Upvotes: 2

Views: 2222

Answers (2)

nikhil mahajan
nikhil mahajan

Reputation: 73

you can do it like this way:-

String myQuery = "select * from person where id=1234567890";

Person personObj = cassandraOperations.selectOne(myQuery, Person.class);

<< For all List<Person> personListObj = cassandraOperations.select(myQuery, Person.class); >>

this work for me using cassandraTemplete object perfectly... didn't try for cassandraOperation.

also you might need @Column(value = "your_columnName_in_DB") if your pojo class's variable name is different

like
@Column(value = "name") private String userName; @Column(value = "age") private int userAge;

revert here if its work?

Also can you help me pass dynamic value to that myQuery string.. using object[] same like prepareStatment in SQL

thanks.

Upvotes: 2

Matthew Adams
Matthew Adams

Reputation: 2197

CassandraTemplate's queryForObject(String,Class) is not meant for arbitrary object mapping. It is modeled after JdbcTemplate's queryForObject(String,Class) method. It's intended to take types that the Cassandra driver can convert directly.

To convert arbitrary application-defined classes, use queryForObject(String,RowMapper<T>) or one of its overloads. CqlTemplate doesn't know how to map arbitrary classes; you have to supply the RowMapper<T> implementation for your class T.

Upvotes: 2

Related Questions