Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 8 DateTime in DbMapper #1454

Closed
hantsy opened this issue Feb 28, 2020 · 9 comments
Closed

Java 8 DateTime in DbMapper #1454

hantsy opened this issue Feb 28, 2020 · 9 comments
Assignees
Labels
bug Something isn't working DB client Helidon DB Client P2

Comments

@hantsy
Copy link

hantsy commented Feb 28, 2020

  • Helidon Version:2.0.m1
  • Helidon SE
  • JDK version: AdaptOpenJDK 11
  • OS: Windows 10
  • Docker version (if applicable):

I used PostgreSQL for tests, the PostgreSQL Jdbc driver supports Java 8 DateTime well.

But when I tried to fetch a TIMESTAMP field to LocalDateTime property, it failed.

created_at TIMESTAMP NOT NULL DEFAULT LOCALTIMESTAMP ,

The DbMapper, it always a java.sql.TimeStamp, I can not convert it as LocalDateTime direclty.

 post.setCreatedAt(createdAt.as(Timestamp.class).toLocalDateTime());

My sample codes.

@m0mus m0mus added DB client Helidon DB Client bug Something isn't working labels Mar 5, 2020
@m0mus m0mus added the P2 label Mar 5, 2020
@Tomas-Kraus
Copy link
Member

OK, this seems to be Helidon DB Client issue. I'll let you know when I know more.
thanks for the report.

@Tomas-Kraus
Copy link
Member

Tomas-Kraus commented Mar 9, 2020

I'm trying to run your sample code.

docker run -it --rm --name pgsql -p 5432:5432 -e POSTGRES_DB=test -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password postgres

Starts PgSQL to be used by jUnit tests, but there is no DB schema and tests are failing with erorrs like

relation "comments" does not exist

Does your sample code have some DB initialization part, or at least SQL script to run?

@hantsy
Copy link
Author

hantsy commented Mar 9, 2020

Use docker-compose.yaml file to serve PostgreSQL service, it includes an init.sql script file

@Tomas-Kraus
Copy link
Member

Thanks, it works now.
What HTTP request shall I use to trigger the issue?

@hantsy
Copy link
Author

hantsy commented Mar 9, 2020

If you replace this line.

with:

post.setCreatedAt(createdAt.as(LocalDateTime.class));

Will get an exception, but according to PostgreSQL JDBC doc. it should work if DbMapper used ResultSet or RowSet in backend.

@Tomas-Kraus
Copy link
Member

I got it:

Caused by: io.helidon.common.mapper.MapperException: Failed to map class java.sql.Timestamp to class java.time.LocalDateTime: Failed to find mapper.
	at io.helidon.common.mapper.MapperManagerImpl.lambda$notFoundMapper$6(MapperManagerImpl.java:128)
	at io.helidon.common.mapper.MapperManagerImpl.map(MapperManagerImpl.java:55)
	at io.helidon.dbclient.jdbc.JdbcStatementQuery$RowPublisher$2.map(JdbcStatementQuery.java:453)
	at io.helidon.dbclient.jdbc.JdbcStatementQuery$RowPublisher$2.as(JdbcStatementQuery.java:447)
	at demo.PostMapper.read(PostMapper.java:29)

when running

curl -X GET http://localhost:8080/posts

on code with few modifications:

    public Post read(DbRow dbRow) {
        var id = dbRow.column("id");
        var title = dbRow.column("title");
        var content = dbRow.column("content");
        DbColumn createdAt = dbRow.column("created_at");
        Post post = new Post();
        post.setId(id.as(UUID.class));
        post.setTitle(title.as(String.class));
        post.setContent(content.as(String.class));
        LocalDateTime createdAtLocDT = createdAt.as(LocalDateTime.class);
        post.setCreatedAt(createdAtLocDT);

        return post;
    }

@Tomas-Kraus
Copy link
Member

The question is, whether this is a bug or a feature. :)
JDBC driver returns this as java.sql.Timestamp and will always do that.
Helidon does not have default mapper for java.sql.Timestamp to java.time.LocalDateTime
I'll implement set of mappers to map java.sql.Timestamp to Calendar/LocalDateTime/ZonedDateTime types.

@Tomas-Kraus
Copy link
Member

Fixed in PR #1485

@Tomas-Kraus
Copy link
Member

PR is merged, closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working DB client Helidon DB Client P2
Projects
Archived in project
Development

No branches or pull requests

3 participants