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
Related
My POJO has a variable annotated like this
#JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
#JsonDeserialize(using = ZonedDateTimeDeserializer.class)
private ZonedDateTime startTs;
//other private variables
My goal is to map this to an Entity for which I'm using ModelMapper and the sample code goes like this
public POJOEntity mapToPOJOEntity(POJO pojo) {
return modelMapper.map(pojo, POJOEntity.class);
}
After mapping when I look at the startTs variable in debug mode it has the desired format but when I check in the db startTs is saving as a whole ZonedDateTime object rather than the desired format I specified in POJO. Did anyone face this issue if yes can someone please help?
Value saved in cosmosDB:
Expected output: "startTs": "2023-01-12T08:58:32.452-06:00"
PS: CosmosDB, spring-boot, Java8, ModelMapper version: 2.3.8
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"/>
I want to get a monthly list, weekly list according to created date but JPA doesn't support LocalDate.
My Code like this:
UserDao:
List<User> findByCreatedAtBetween(LocalDate start, LocalDate end);
UserEntity:
#Column(nullable = false, updatable = false)
#Temporal(TemporalType.TIMESTAMP)
#CreatedDate
private LocalDate createdAt;
#Column(nullable = false)
#Temporal(TemporalType.TIMESTAMP)
#LastModifiedDate
private LocalDate updatedAt;
But createdAt and updatedAt only supports java.util.Date. Why doesn't it support java.time.LocalDate?
As you found out LocalDate is currently not supported by Spring Data Envers, or actually in Spring Data Commons where the relevant code resides.
The reason for this is that these timestamp fields are intended to, well, be time stamps, i.e. specify points in time. LocalDate doesn't do that. The problem is not so much the lack of accuracy, but that LocalDate and its sibling LocalDateTime do not define an ordering. It is perfectly possible that something that happened on the Jan 1st, 1970 happened after something else that happened on Jan 2nd, 1970. For more detail refer to the JavaDoc or to http://blog.schauderhaft.de/2018/03/14/dont-use-localdatetime/
Note that LocalDateTime is supported, but mostly because that was implemented before we did realize that it is the wrong type to use for this purpose.
I highly recommend using Instant as the type for this kind of fields.
I have a Spring Batch Job that defines a JdbcPagingItemReader with a BeanPropertyRowMapper :
JdbcPagingItemReader<RawNotice> reader = new JdbcPagingItemReader<>();
final SqlPagingQueryProviderFactoryBean sqlPagingQueryProviderFactoryBean = new SqlPagingQueryProviderFactoryBean();
sqlPagingQueryProviderFactoryBean.setDataSource(dataSource);
sqlPagingQueryProviderFactoryBean.setSelectClause("select *");
sqlPagingQueryProviderFactoryBean.setFromClause("from a_table");
sqlPagingQueryProviderFactoryBean.setWhereClause("state = :state");
sqlPagingQueryProviderFactoryBean.setSortKey("id");
Map<String, Object> parameters = new HashMap<>();
parameters.put("state", "interesting_state");
reader.setQueryProvider(sqlPagingQueryProviderFactoryBean.getObject());
reader.setDataSource(dataSource);
reader.setPageSize(10);
// The line below is the interesting one
reader.setRowMapper(new BeanPropertyRowMapper<>(MyEntity.class));
reader.setParameterValues(parameters);
return reader;
This used to work fine, but since we upgraded to spring boot 1.4 and spring cloud Camden, it throws an exception :
org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type [java.sql.Timestamp] to required type [org.joda.time.LocalDateTime] for property 'ADateColumn'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.sql.Timestamp] to required type [org.joda.time.LocalDateTime] for property 'ADateColumn': no matching editors or conversion strategy found
The column ADateColumn is declared as a Joda LocalDateTime and stored as a java.sql.Timestamp in the database.
I'm quite aware I could add my own joda converters to the BeanPropertyRawMapper conversionService for example, or create a PropertyEditor that understands Java LocalDateTime, but that looks rather like a configuration problem, like something isn't being registered right.
Anybody with a solution/suggestion to fix this problem ?
Thanks !
This is the part of the entity that poses problem :
#Entity
#EqualsAndHashCode(of = { "..." })
#ToString(of = { .... })
public class MyEntity {
#Getter
#Setter
#Id
#GeneratedValue
private Long id;
#Getter
#Version
#Column(nullable = false)
private int version;
//<--- snip --->
#Getter
#Setter
#Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime aDateColumn;
}
Hibernate is version 4.3.11.Final
JPA is version 2.1 with Hibernate Entity Manager 4.3.11.Final
So I finally ended up creating my own BeanPropertyRowMapper with custom Joda converters :/
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