spring-projects/spring-data-cassandra — pull request #19 by chbatey (2014-08-16 09:07:11+00:00)
Title: Throw IllegalArgumentException if a column is requested that isn't in the table

I was evaluating spring-data-cassandra and I made the mistake of overriding the column name in the @Column when the column in question is part of the @PrimaryKey e.g

@PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED)
@Column("app_name")
private String appName;

This gave me an ArrayOutOfBoundsException from the Datastax Java driver

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
at com.datastax.driver.core.ColumnDefinitions.checkBounds(ColumnDefinitions.java:284)
at com.datastax.driver.core.ArrayBackedRow.isNull(ArrayBackedRow.java:51)
at org.springframework.data.cassandra.convert.ColumnReader.get(ColumnReader.java:42)
at org.springframework.data.cassandra.convert.ColumnReader.get(ColumnReader.java:37)
at org.springframework.data.cassandra.convert.ColumnReader.get(ColumnReader.java:30)

This also happens if you have it in the @PrimaryKeyColumn but typo it.

A longer term solution may be to do some validation of the Java object vs the table but in the mean time to allow people to quickly diagnose this I've just added an IllegalArgumentException with a nice error message so new adopters quickly fix it and move on e.g

java.lang.IllegalArgumentException: Column does not exist in Cassandra table: appname
at org.springframework.data.cassandra.convert.ColumnReader.getColumnIndex(ColumnReader.java:147)
at org.springframework.data.cassandra.convert.ColumnReader.get(ColumnReader.java:37)

Keep up the good work - awesome project