Hibernate4 timestamp with joda time - spring

does any one know about how to implement create & update timestamps with spring mvc4, hibernate4 and joda-time
#CreationTimestamp
#Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime created_at;
public void setCreated_at(LocalDateTime created_at) {
this.created_at = created_at;
}
When i tried in the above way, i got errors

Related

ZonedDateTime retrieved with one hour shift in Hsqldb with 2.6.x

I use Spring boot and JPA and my Order domain object have this field:
#Column(name = "TIMESTAMP")
private ZonedDateTime timestamp;
I create a date with this method:
protected static ZonedDateTime createZonedDateTime(final String date) {
LocalDateTime ldt = LocalDateTime.parse(date + " 00:00", DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"));
return ldt.atZone(ZoneId.systemDefault());
}
When I insert this in database throw JPA, with showsql to true, I can see that the inserted date is: [2020-01-01T00:00+01:00[Europe/Paris]]
when I retrieve the data throw JPA repository, the date I get is: 2020-01-01T01:00+01:00[Europe/Paris]
How come I have one hour shift? It's not working since hsqldb 2.6.0. Works perfectly with 2.5.0.
I use Liquidbase to create the field with:
<column name="TIMESTAMP" type="TIMESTAMP WITH TIME ZONE"
remarks="The timestamp of the order"/>

How can i convert Date to epoch (unix timestamp) in Spring boot2 with mongoDB?

I'm using Spring boot 2 with mongoDB.
Currently, With jackson serilization, Dates from API response serialized to milliseconds.
{
"main_reg_dt": 1515485462433
}
After searching on stackoverflow, added this option to application's configuration applcation.yaml to this
WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS: true
but the result is same as before.
this is how the model is implemented
#JsonInclude(JsonInclude.Include.NON_DEFAULT)
#QueryEntity
public class Card {
#Id
#JsonProperty("id")
#Field("_id")
String id;
#JsonProperty("main_reg_dt")
#Field("main_reg_dt")
Date registeredDateTime;
}
How can i convert Date to epoch (unix timestamp) in Spring boot2 with mongoDB?
I found a way to change serialization method.
First, create custom serializer
public class CustomDateSerializer extends JsonSerializer {
#Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
long formattedDate = ((Date) o).getTime() / 1000;
jsonGenerator.writeString(String.valueOf(formattedDate));
}
}
Then, add annotation for using customized serializer
#JsonProperty("main_reg_dt")
#Field("main_reg_dt")
#JsonSerialize(using = CustomDateSerializer.class)
Date registeredDateTime;
Afther this date in response will be serialized as unix timestamp

spring boot, jackson and localdate

I use spring boot with mysql
in my application.properties
spring.jpa.generate-ddl=true
spring.jackson.serialization.write-dates-as-timestamps=false
In my build.gradle I have
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-web')
compile 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
In my java class
import java.time.LocalDate;
#Entity
public class WebSite implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long webSiteId;
private LocalDate date;
...
}
When this table is created,
date field is created like a TINYBLOB
Why is not a date
This is not an issue with Jackson, but rather that whatever you are using for ORM doesn't know how to convert a Java LocalDate to a MySQL Date.
There are two ways to do this. If you are using Hibernate, you simply include org.hibernate:hibernate-java8 in your dependencies.
Alternatively, if you want just use JPA, you need to create an Attribute Converter. For example:
#Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {
#Override
public Date convertToDatabaseColumn(LocalDate locDate) {
return (locDate == null ? null : Date.valueOf(locDate));
}
#Override
public LocalDate convertToEntityAttribute(Date sqlDate) {
return (sqlDate == null ? null : sqlDate.toLocalDate());
}
}
The Attribute Converter will handle converting between a Java LocalDate and MySQL Date.
See: http://www.thoughts-on-java.org/persist-localdate-localdatetime-jpa/

Spring Data JPA set LocalDate format

I'm trying to set LocalDate (and LocalDateTime) format in database like dd-MM-yyyy which is very useful sometimes. Instead I'm getting something like this in H2 DB.
aced00057372000d6a6176612e74696d652e536572955d84ba1b2248b20c00007870770e05000007e1010c0e1c1837131f0078
I tried to use #DateTimeFormat(pattern = "dd-MM-yyyy HH:mm") but nothing changes.
#CreatedDate
#DateTimeFormat(pattern = "dd-MM-yyyy HH:mm")
private LocalDateTime created;
I'm using Spring MVC with Spring Data BOM Hopper-SR1 (1.10.1) and H2 DB 1.3.156
I added package to scan in configuration #Bean LocalContainerEntityManagerFactoryBean
entityManagerFactoryBean.setPackagesToScan("com.jpomykala.model", "org.springframework.data.jpa.convert.threeten");
Now it's simple enough: 2017-01-12 14:55:42.238

how to avoid rejected value [] issue in spring form submission

i have two dates in form submission in Spring 3 + Hibernate.
#Column(name = "FinStartDate")
private Date finStartDate;
#Column(name = "FinEndDate")
private Date finEndDate;
I'm display/hide dates on the basis of some criteria. When the dates are hidden and submit the form, the following errors
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'register' on field 'obj.finEndDate': rejected value []; codes [typeMismatch]
How to avoid the issue.
#JsonDeserialize(using = LocalDateDeserializer.class)
#JsonSerialize(using = LocalDateSerializer.class)
#DateTimeFormat(pattern = "dd.MM.yyyy")
private Date finEndDate;
Maybe, you should use serializer/deserializer.
I think that you miss a formatter to convert the date String to a Date object.
You can try to annotate your field
#DateTimeFormat(pattern = "yyyy-MM-dd")
or to declare a initbinder in your controller like :
#InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));
}
Or you can declare a formatter in you mvc configuration file that will format every Date object your application is binding to.
Add #DateTimeFormat annotation for following way. If not working update date format. (MM-dd-yyyy, dd-MM-yyyy)
#Column(name = "FinEndDate")
#DateTimeFormat(pattern = "yyyy-MM-dd")
private Date finEndDate;

Resources