- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Spring Boot 3 (Hibernate 6.1) 与 PostgreSQL 的结合似乎有一个问题,Instant
值映射到 SQL timestamp
s (without time zone
),因为下面的测试用例失败了。读取值与写入值的差异在于本地时区偏移量。
测试用例在使用H2测试数据库或切换回Spring Boot 2.7.6(Hibernate 5.6)时成功执行。
JPA 实体:
@Entity
public class MyEntity {
@Id
UUID id = UUID.randomUUID();
//@JdbcType(TimestampJdbcType.class) //work-around to get it somehow working
//(alternative: declare this globally in a custom Hibernate dialect)
//but why is this neccessary only with spring-boot-3 and postgres?
@Column(columnDefinition = "timestamp")
Instant createdTimestamp = Instant.now();
}
Spring 数据存储库:
public interface MyEntityRepository extends CrudRepository<MyEntity, UUID> {
}
JUnit 集成测试:
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE) //comment out to use the H2 database
class MyEntityRepositoryITest {
@Autowired
MyEntityRepository myEntityRepository;
@Test
@Transactional(propagation = NOT_SUPPORTED) //do not use a transaction
void readInstantSameAsPersisted() {
var entity = new MyEntity();
myEntityRepository.save(entity);
var read = myEntityRepository.findById(entity.id).orElseThrow();
assertEquals(entity.createdTimestamp.getEpochSecond(), read.createdTimestamp.getEpochSecond());
}
}
Maven pom.xml:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
Spring 应用程序.properties:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
spring.datasource.username=username
spring.datasource.password=password
注意事项:
spring.jpa.properties.hibernate.jdbc.time_zone
设置为任何值都没有帮助。LocalDateTime
是一个选项,因为我希望 createdTimestamp
代表一个明确的时间点。spring.datasource.hikari.connection-init-sql=SET TIME ZONE 'UTC'
也可以作为一种解决方法,但我不明白为什么这是必要的——阅读基于自 1970-01-01 以来的秒数的值应该给出与写入值相同的结果,无论在幕后使用哪个区域。Instant
,但支持 Hibernate does . This answer说 timestamp with time zone
确实(不像名字暗示的那样)实际上 不 携带额外的时区信息,只是“在 UTC 时间线上存储一个点”,可以用不同的区域,这似乎更适合我的用例。
最佳答案
The PostgreSQL documentation说:
For
timestamp with time zone
, the internally stored value is always in UTC... An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone.
如果您将该字段声明为 timestamp with time zone
,或者简单地声明为 timestamptz
,那么您将达到您想要的效果。它也将满足您在数据库中不要有多个时区的要求。
关于java - 即时时间戳与 Spring Boot 3 结合 PostgreSQL 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74866474/
给定一个带有多个 date_time 戳的字符串,我想 提取第一个戳及其前面的文本 候选字符串可以有一个或多个时间戳 后续的 date_time 戳记将被 sep="-" 隔开 后续date_time
是否可以合并从相机拍摄的文本和照片?我想在照片上标记日期和时间,但我在 Google 上找不到任何内容。 最佳答案 使用下面的代码来实现你所需要的。 Bitmap src = Bitm
有没有办法通过 Graph API 戳另一个用户?基于this post ,并使用 Graph Explorer ,我发布到“/USERID/pokes”,我已经授予它(Graph API 应用程序和
我有两个向左浮动的元素。一个是 body 的第一个 child ,另一个是容器的第一个 child ,容器是 body 的第二个 child 。 ...
我是一名优秀的程序员,十分优秀!