Hibernate Joda time DateTime在数据库中的存储不正确

Hibernate Joda time DateTime在数据库中的存储不正确,hibernate,grails,groovy,gorm,jodatime,Hibernate,Grails,Groovy,Gorm,Jodatime,我使用org.jadira.usertype:usertype.JodaTime:1.9将JodaTimeDateTime字段存储到timestamtz列中。应用服务器有+4个时区。DB服务器+9时区new DateTime()产生${currentTime+1hour}+9其中+9是时区(正确的值是${currentTime+5hours)+9) 我没有找到任何相关主题。java.util.Date存储正确 域对象具有以下映射属性: static mapping = { dateCre

我使用
org.jadira.usertype:usertype.JodaTime:1.9
将JodaTime
DateTime
字段存储到
timestamtz
列中。应用服务器有+4个时区。DB服务器+9时区
new DateTime()
产生
${currentTime+1hour}+9
其中+9是时区(正确的值是
${currentTime+5hours)+9

我没有找到任何相关主题。
java.util.Date
存储正确

域对象具有以下映射属性:

static mapping = {
    dateCreated sqlType:'timestamptz'
}

如何正确存储日期时间?

试着用-Duser启动JVM。timezone=UTC to JAVA_选择这样的方式,时间在一个区域内,然后您可以在该区域上执行操作,将其转换到您所在的位置。

好的。我已经花了8个小时来解决问题。如果您使用项目来持久化JodaTime,您必须设置
数据库
PersistentDateTime
类的ne
属性等于当前应用程序服务器时区(不是数据库!)

但是使用它更好。它解决了这个问题,因为它使用
java.utl.Date
来持久化
DateTime
并且
java.util.Date
默认情况下正确持久化Fedor

我假设您使用的是带有时区列的Oracle时间戳,对吗?Usertype还不支持这种类型(即带有时区),因为数据库之间对JDBC的处理不同,尽管项目很乐意接受修补程序


这里有一些关于Oracle的好讨论:

我通过创建其他PersistentDateTime extends AbstractVersionableUserType解决了这个问题

import java.sql.Timestamp;

import org.hibernate.SessionFactory;
import org.hibernate.usertype.ParameterizedType;
import org.jadira.usertype.dateandtime.joda.columnmapper.TimestampColumnDateTimeMapper;
import org.jadira.usertype.spi.shared.AbstractVersionableUserType;
import org.jadira.usertype.spi.shared.IntegratorConfiguredType;
import org.joda.time.DateTime;

public class PersistentDateTime extends AbstractVersionableUserType<DateTime, Timestamp, TimestampColumnDateTimeMapper> implements ParameterizedType, IntegratorConfiguredType {

@Override
public int compare(Object o1, Object o2) {
    return ((DateTime) o1).compareTo((DateTime) o2);
}

@Override
public void applyConfiguration(SessionFactory sessionFactory) {

    super.applyConfiguration(sessionFactory);

    TimestampColumnDateTimeMapper columnMapper = (TimestampColumnDateTimeMapper) getColumnMapper();
    columnMapper.setDatabaseZone(null);
    columnMapper.setJavaZone(null);
}
}
导入java.sql.Timestamp;
导入org.hibernate.SessionFactory;
导入org.hibernate.usertype.ParameterizedType;
导入org.jadira.usertype.dateandtime.joda.columnmapper.TimestampColumnDateTimeMapper;
导入org.jadira.usertype.spi.shared.AbstractVersionableUserType;
导入org.jadira.usertype.spi.shared.IntegratorConfiguredType;
导入org.joda.time.DateTime;
公共类PersistentDateTime扩展AbstractVersionableUserType实现ParameterizedType、IntegratorConfiguredType{
@凌驾
公共整数比较(对象o1、对象o2){
返回((日期时间)o1),与((日期时间)o2)比较;
}
@凌驾
公共无效应用程序配置(SessionFactory SessionFactory){
超级应用程序配置(会话工厂);
TimestampColumnDateTimeMapper columnMapper=(TimestampColumnDateTimeMapper)getColumnMapper();
columnMapper.setDatabaseZone(null);
setJavaZone(null);
}
}

我也遇到了同样的问题。在config中指定app和db区域解决了这个问题

            <prop key="jadira.usertype.autoRegisterUserTypes">true</prop>
            <prop key="jadira.usertype.databaseZone">America/Los_Angeles</prop>
            <prop key="jadira.usertype.javaZone">America/Los_Angeles</prop>
true
美国/洛杉矶
美国/洛杉矶

只需设置JPA属性:

<property name="jadira.usertype.autoRegisterUserTypes"
          value="true"/>
<property name="jadira.usertype.databaseZone"
          value="jvm"/>
<property name="jadira.usertype.javaZone"
          value="jvm"/>

只是针对Spring Boot用户的更新。我可以执行以下操作:

spring.jpa.properties.jadira.usertype:
  autoRegisterUserTypes: true
  databaseZone: jvm
  javaZone: jvm

我把它放在我的
application.yaml
文件中。

我们使用的是PostgreSQL,但官方的hibernate支持并没有扩展到hibernate 4.0:-(为此,您需要使用usertype。这是一个很好的方法-但是请注意,如果您已经在数据库的另一个时区中存储了日期,则不能这样做。换句话说,对于新项目没有问题,但是对于具有现有数据的项目,要非常非常小心地这样做。这个文件放在什么文件中?这个文件放在什么文件中?@dgrant在您的persis中tence.xml文件没有xml文件有没有办法做到这一点?到目前为止,我的项目正在使用纯注释进行配置。。。