1

我有一个Quarkus应用程序,我在其中使用Vert.x Reactive PostgreSQL Client持久化数据。

这是要持久化的实体 bean:

public class SomeEntity {
  public Long id;
  public Integer f1;
  public Boolean f2;
  public SomeType f3;
  public OffsetDateTime f4;
  ...

问题是f4type 的字段OffsetDateTime。在持久化之前,它的值2020-09-11T19:07:46.822828+02:00与偏移量+02:00Berlin GMT+2

当这条记录被保存到 PotgreSQL 中时,我看到值(在 pgAdmin 4 中)被保存为2020-09-11 17:07:46.822828+00(在 UTC 中没有偏移量)。当我取回它时,偏移量消失了。

我想用时区偏移量找回它。

这是我用于持久化实体的代码片段:

@QuarkusTest
public class SomeEntityRepositoryTest {

  static final Duration TIMEOUT = Duration.ofSeconds(2);

  @Inject
  Pool client;

  private Long idToGet;

  @BeforeEach
  public void initDatabase() {
    idToGet = createOne(new SomeEntity(null, 17983,
        false, SomeType.values()[0],
        OffsetDateTime.now()));
  }


  protected SomeEntity from(Row row) {
    return new SomeEntity(row.getLong("id"),
        row.getInteger("f1"),
        row.getBoolean("f2"),
        SomeType.valueOf(row.getString("f3")),
        row.getOffsetDateTime("f4"));
  }

  protected Long createOne(SomeEntity someEntity) {
    return client.preparedQuery("INSERT INTO SOME_ENTITY (f1, f2, f3, f4) "
        + "VALUES ($1, $2, $3, $4) "
        + "RETURNING id, f1, f2, f3, f4")
        .execute(Tuple.of(someEntity.f1,
            someEntity.f2,
            someEntity.f3.toString(),
            someEntity.f4))
        .map(RowSet::iterator)
        .map(iterator -> iterator.hasNext() ? iterator.next() : null)
        .map(this::from)
        .map(inserted -> inserted.id)
        .await()
        .atMost(TIMEOUT);
  }

这是数据库创建脚本:

CREATE TABLE SOME_ENTITY (
  id SERIAL PRIMARY KEY, 
  f1 integer, 
  f2 boolean, 
  f3 nvarchar, 
  f4 timestamptz
)

这些是我的依赖项build.gradle

dependencies {
    implementation enforcedPlatform("io.quarkus:quarkus-bom:1.7.3.Final")
    implementation 'io.quarkus:quarkus-resteasy'

    testImplementation 'io.quarkus:quarkus-junit5'
    testImplementation 'io.rest-assured:rest-assured'

    implementation "io.quarkus:quarkus-reactive-pg-client"
    implementation "io.quarkus:quarkus-vertx"
    implementation "io.quarkus:quarkus-resteasy"
    implementation "io.quarkus:quarkus-resteasy-jackson"
    implementation "io.quarkus:quarkus-resteasy-mutiny"
    implementation "io.netty:netty-transport-native-epoll"
    implementation "org.apache.commons:commons-lang3"
    implementation "io.quarkus:quarkus-liquibase"
    implementation "io.quarkus:quarkus-jdbc-postgresql"
}

4

0 回答 0