I am using below Datastax versions with Java 8:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>3.7.2</version>
</dependency>
My table has a Date column as below:
cass_table (
data_source_id int,
company_id text,
create_date date)
When I trying to save the data into C* table as below:
final IndustryCompany four = new IndustryCompany(1,1236, ProdUtils.today());
industryCompanyRepository.save(one);
public static Date today() {
return java.sql.Date.valueOf(new SimpleDateFormat(ProducerConstants.DATABASE_DATE_FORMAT).format(Calendar.getInstance().getTime()));
}
Getting error:
Caused by: com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [date <-> java.sql.Date]
at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:57) ~[cassandra-driver-core-3.7.2.jar:na]
What am I doing wrong here?
You need to pass variable with com.datastax.driver.core.LocalDate
type instead. See the documentation for mapping between Java type and CQL types. You can use LocalDate
from JDK 8, or from Joda time package if you'll use so-called optional codec.
Another possibility is to write date
<->java.sql.Date
and register it for direct mapping.