【问题标题】:How to store only specific timestamp columns in UTC in hibernate如何在休眠中仅存储UTC中的特定时间戳列
【发布时间】:2018-08-19 10:28:24
【问题描述】:

在我的 Mysql DB 中,我有一些列需要使用休眠以 UTC 存储。我不能强制我的 JVM 以 UTC 格式存储所有时间戳列,因为我有其他列用于不同的时区。我找到了两种解决方案

1) To configure hostname as jdbc:mysql://hostname/databaseName?useTimezone=true&serverTimezone=UTC 

2)  private static final TimeZone UTC = TimeZone.getTimeZone("UTC");

但是这两种解决方案都是针对所有列强制使用 UTC 时区,而我的要求是针对特定列。

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false,updatable = false, name="created_at")
    private Date createdAt;

【问题讨论】:

  • as I have other columns for different time zones ... 为什么不将所有内容都存储为 UTC?
  • 我使用的是多租户数据源,不同的列需要存储在不同的时区
  • @Himanshu 如果我的回答对你有帮助,请采纳。 :)
  • @PankajSinghal 谢谢它的工作
  • 很高兴为您提供帮助 :)

标签: mysql hibernate hibernate-mapping


【解决方案1】:

您可以在代码中手动执行此操作,特别是 1 列。基本上,这个想法是在数据库中更新之前将日期更新为所需的日期。

为此,您需要在 Hibernate 的 EventListenerRegistry 中分配一个 listener。以下类将在执行保存操作之前应用CreateOrUpdateDateListener 侦听器(由您创建)。

@Component
public class HibernateEventWiring {

    @Autowired
    @Qualifier(value = "your_sessionFactory")           //Your session factory
    private LocalSessionFactoryBean sessionFactory;

    @Autowired
    private CreateOrUpdateDateListener listener;

    @PostConstruct
    public void registerListeners() {
        EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory.getObject()).getServiceRegistry().getService(
                EventListenerRegistry.class);
        registry.getEventListenerGroup(EventType.SAVE_UPDATE).appendListener(listener);
    }
}

以下类扩展了 DefaultSaveOrUpdateEventListener 并添加了将时间戳更新为所需内容的自定义代码。而且,我们在EventListenerRegistry 中使用了它的对象作为监听器

@Component
public class CreateOrUpdateDateListener extends DefaultSaveOrUpdateEventListener {

    @Override
    public void onSaveOrUpdate(SaveOrUpdateEvent event) {
        if (event.getObject() instanceof CreateUpdateTimestamp) {
            CreateUpdateTimestamp record = (CreateUpdateTimestamp) event.getObject();
            record.setUpdatedTimestamp(LocalDateTime.now(Clock.systemUTC()));
        }
        super.onSaveOrUpdate(event);
    }
}

public interface CreateUpdateTimestamp {
    public void setCreatedTimestamp(LocalDateTime date);
    public void setUpdatedTimestamp(LocalDateTime date);
}

以下是实体类。这个类将实现CreateUpdateTimestamp,这样我们就可以通过CreateUpdateTimestampCreateOrUpdateDateListener中的引用来使用这个类。

@Entity
@Table(name = "transactions")
public class Transaction implements CreateUpdateTimestamp{

    private LocalDateTime updatedTimestamp;

    @Column(name = "updated_timestamp", insertable = true, updatable = true)
    public LocalDateTime getUpdatedTimestamp() {
        return updatedTimestamp;
    }
    public void setUpdatedTimestamp(LocalDateTime updatedTimestamp) {
        this.updatedTimestamp = updatedTimestamp;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2015-12-18
    • 2017-09-11
    • 1970-01-01
    • 2015-04-06
    • 2017-10-05
    • 2011-01-03
    相关资源
    最近更新 更多