Unable to retrieve the data from datastax having timestamp field - java

I'm having the below table in the keyspace myks
CREATE table IF NOT EXISTS myks.users (
user_name text,
email text,
created_at timestamp,
updated_at timestamp,
PRIMARY KEY (user_name)
);
Below is the model class
#Table(value = "users")
public #Data class Users{
#PrimaryKey
#Column("user_name")
#CassandraType(type = DataType.Name.TEXT)
private String user_name;
#Column("email")
#CassandraType(type = DataType.Name.TEXT)
private String email;
#Column("created_at")
#CassandraType(type = DataType.Name.TIMESTAMP)
private Timestamp created_at;
#Column("updated_at")
#CassandraType(type = DataType.Name.TIMESTAMP)
private Timestamp updated_at;
}
Repository interface
#Repository
public interface UsersRepository extends CrudRepository<Users, String> {
}
Inserted the below values into the table
Users users = new Users();
LocalDateTime ldt_created = LocalDateTime.now();
LocalDateTime ldt_updated = ldt_created.plus(1000, ChronoUnit.MILLIS);
Timestamp ts_created = Timestamp.valueOf(ldt_created);
Timestamp ts_updated = Timestamp.valueOf(ldt_updated);
users.setUser_name(krishna");
users.setEmail("krishna#gmail.com");
users.setCreated_at(ts_created);
users.setUpdated_at(ts_updated);
usersRepository.save(users);
It got saved in the table but while retrieving the data it is throwing the below exception
No converter found capable of converting from type [java.util.Date] to type [java.sql.Timestamp]

CQL's timestamp type is mapped into java.util.Date Java type, so you need to use it instead of Timestamp. See CQL to Java mapping table for this & other types.
You can also use so-called optional codecs (an additional dependency) to map timestamp into other Java types, such as, Instant. See documentation for more information.

java.util.Date is no longer used in driver 4.X java-driver-mapper-processor version. Java.time.Instant is used instead.
Mapping in java.util.Date doesn't work anymore, and throws exception:
Field: expiration_date, Entity Type: java.util.Date, CQL type: TIMESTAMP
So actual solution is to use Java.time.Instant for 4.X, and java.util.Date for 3.X.

Related

findByDate in spring boot jpa gives empty list

I'm using spring boot JPA for CRUD operations. I'm querying the database table with the field name created_at which is of type date.There are some rows in the table with the given date but JPA is giving zero result set. I'm using Oracle 11g
Here is my entity
import java.sql.Date;
#Entity
#Table(name="veev_json")
public class VeevJson {
#Id
#Column(name="ID")
private int id;
#Column(name="CREATED_AT")
private Date createdDate;
}
My JPA Repository
import java.util.Date;
#Repository
public interface VeevJsonRepository extends JpaRepository<VeevJson, Integer> {
public List<VeevJson> findByCreatedDate(Date date);
}
Calling the function in the service layer
Date date = new Date(); //taking current date of type java.util.Date
List<VeevJson> documents = veevJsonRepository.findByCreatedDate(date);
My DB table structure
ID NUMBER(10,0)
CREATED_AT DATE
SQL query generated by the hibernate:
select veevjson0_.ID as ID1_1_, veevjson0_.CREATED_AT as CREATED_AT2_1_, veevjson0_.JSON as JSON3_1_, veevjson0_.STATUS as STATUS4_1_ from veev_json veevjson0_ where veevjson0_.CREATED_AT=?
When using a field with type Date, you should also use the #Temporal annotation. The default value of #Temporal is TemporalType.TIMESTAMP, and your JPA implementation may get confused about dealing with a field of type java.util.Date, passing as argument of query the timestamp instead of date.
Please annotate your field as
import java.util.Date;
#Entity
#Table(name = "veev_json")
public class VeevJson {
#Id
#Column(name = "ID")
private int id;
#Temporal(TemporalType.DATE)
#Column(name = "CREATED_AT")
public Date createdDate;
...
}
Doing so will allow JPA implementation to send as queried value only the date (probably in 'YYYY-MM-dd' format) instead of timestamp or any other value.
If you prefer and your JDBC supports 4.1 version, you may exchange the java.util.Date for java8 time API types, but I guess this is out of scope here.

Spring JPA entity mapping LocalDate and LocalDateTime field to Sql Server

I am using Spring JPA and have mapped the following entity to H2 Database fields:
#Entity
#Table(name = "REQUEST")
public class Request implements Serializable {
#Id
#Column(name = "ID")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name = "CORR_ID")
private String corrId;
#Column(name = "CB_DATE")
private LocalDate cbDate;
#Column(name = "ACTIVE")
private boolean active;
#Column(name = "CREATED_DATE")
private LocalDate createdDate;
#Column(name = "LAST_UPDATED")
private LocalDateTime lastUpdated;
}
-- H2 DB Script
CREATE TABLE REQUEST
(
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
CORR_ID CHAR(36) NOT NULL,
CB_DATE DATE NOT NULL,
ACTIVE BOOLEAN DEFAULT FALSE NOT NULL,
CREATED_DATE DATE NOT NULL,
LAST_UPDATED DATETIME NOT NULL
);
I am unsure if the following SQL Server column definitions will map correctly to Java. I have seen that BIT maps to boolean java type, However, I am unsure about Date & DateTime will be mapped correctly to LocalDate & LocalDateTime respectively.
--MS SQL SEVER SCRIPT
CREATE TABLE REQUEST
(
ID BIGINT AUTO_INCREMENT PRIMARY KEY,
CORR_ID CHAR(36) NOT NULL,
CB_DATE DATE NOT NULL,
ACTIVE BIT DEFAULT FALSE NOT NULL,
CREATED_DATE DATE NOT NULL,
LAST_UPDATED DATETIME NOT NULL
);
Some clarity around this will be appreciated. Do I need to make any adjust to my Java code? Are my SQL server definitions ok?
I am doing conversion before saving with JPA:
re.setIsActive(tt.isActive());
re.setCbDate(LocalDate.parse(tt.getCobDate()));
re.setCreatedDate(LocalDate.now());
I guess the version of the SQL Server itself, the SQL Server JDBC Driver and the used Hibernate Dialect play here a role to determine what types are correct or acceptable on the SQL Server side.
That's too many variables to say something. I would let Hibernate create the table and see what it generates. You need to determine which SQL Server version(s) you have to support. Based on that i would choose the best dialect and JDBC Driver and then again see what Hibernate will generate with this setup and take this as the correct types.

#DateTimeFormat and TO_DATE() pattern for Calendar type passed through rest service

I'm working on Web application which is using Spring Data JPA and Oracle Database. I was using #RepositoryRestResource annotation in interface where I was just declaring some query methods with named parameters using #Param and #Query annotations. Today I needed to add a new entity with the dates. In database one column is type of DATE and the other one is type of TIMESTAMP. And below the Java representation of this two columns only, of course with all setters and getters, but it has more fields so just adding this:
#Temporal(TemporalType.DATE)
#Column(name = "INIT_DATE")
private Calendar initDate;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "AGG_DATE")
private Calendar aggDate;
I also created new interface for case, the same way as always:
#RepositoryRestResource(collectionResourceRel = "customer", path = "customer")
public interface ICustomerRepository extends PagingAndSortingRepository<Customer, Long> {
#Query("SELECT c FROM Customer c where c.initDate <= TO_DATE(:currentDate, 'yyyy-MM-dd') AND c.aggDate >= TO_DATE(:currentDate, 'yyyy-MM-dd')")
public List<Customer> filterByDate(#Param("currentDate") #DateTimeFormat(pattern = "yyyy-MM-dd") Calendar currentDate);
}
I also tried other format, but I'm receiving this error:
ORA-01830: date format picture ends before converting entire input string
I'm trying to get this data from database using this http request:
http://localhost/webApp/customer/search/filterByDate?currentDate=2017-07-10
And to be honest, I have no idea what is the problem here... The format date in the database is yy/MM/DD, but it also wasn't working for me... Could you tell me what I'm missing or doing wrong??

Mapping MapOf - UserDefinedType to POJO in cassandra using Spring Data Cassandra

I have the following user defined type in cassandra:
CREATE TYPE key(
name text,
created_time timestamp
);
It goes in the following table:
CREATE TABLE keys(
id uuid,
keys map<text, frozen<list<key>>>
);
** Note that the below POJOS also contain setters/getters, snipped for readability**
Then I have the following POJO for mapping for key:
#UserDefinedType(value = "key")
public class MyKey {
#Column(value = "name")
private String name;
#Column(value = "created_time")
private Instant createdTime;
}
I have the following POJO for keys
#Table(value = "keys")
public class MyKeys {
#PrimaryKey(value = "id")
private UUID id;
#Column(value = "keys")
private Map<String, List<Key>> keys;
}
Unfortunately when I try to insert an instance of MyKeys I get the following error:
org.springframework.dao.InvalidDataAccessApiUsageException: Only primitive types are allowed inside Collections for property [keys] of type [interface java.util.Map] in entity [com.utx.dao.tables.MyKeys]
Does the Spring-Data-Cassandra mapper not work with maps of user defined types, and if not, is there a solution to my problem?

Spring Data (Rest): Date serialization mapping returns a wrong value

I use Spring Boot and Data Rest to create a simple microservice in Java8 and get a wrong serialized value in a Date attribute in my JSON response.
My entity:
#Entity
public class ArchivedInvoice implements Serializable {
...
#Column
private java.util.Date invoiceDate;
...
}
My repository interface:
#RepositoryRestResource(collectionResourceRel = "archivedinvoices", path = "archivedinvoices")
public interface ArchivedInvoiceRepository extends PagingAndSortingRepository < ArchivedInvoice, Long > {
...
#RestResource(rel = "findByDate", path = "findByDate")
public Page< ArchivedInvoice > findByInvoiceDate(#Param("invoiceDate") #Nullable #DateTimeFormat(iso = ISO.DATE) Date invoiceDate, Pageable pageable);
...
}
Postgres saves the attribute in a simple date (invoice_date date NOT NULL - '2016-02-22') but the JSON response returns:
"invoiceDate" : "2016-02-21T23:00:00.000+0000"
How can I avoid this?
java.util.Date is actually a timestamp:
The class Date represents a specific instant in time, with millisecond
precision.
Use java.sql.Date instead if the SQL type is date.
Or if you use java 8, you can try using java.time.LocalDate. For that to work you will need to register Springs JSR310 JPA converters.

Categories