相关疑难解决方法(0)

如何在Hibernate中映射Interval类型?

在PostgreSQL下,我PersistentDuration用于sql类型间隔和持续时间之间的映射,但它不起作用.

另一位用户发现了同样的问题,并附带了自己的课程:

public void nullSafeSet(PreparedStatement statement, Object value, int index) 
        throws HibernateException, SQLException { 
    if (value == null) { 
        statement.setNull(index, Types.OTHER); 
    } else { 
        Long interval = ((Long) value).longValue(); 
        Long hours = interval / 3600; 
        Long minutes = (interval - (hours * 3600)) / 60; 
        Long secondes = interval - (hours * 3600) - minutes * 60; 
            statement.setString(index, "'"+ hours +":" 
                    + intervalFormat.format(minutes) + ":" 
                    + intervalFormat.format(secondes)+"'"); 

    } 
}
Run Code Online (Sandbox Code Playgroud)

但它不适用于真实格式,因为它假设间隔模式只是"hh:mm:ss".事实并非如此:见

这里有一些我需要从数据库中解析的实例:

1 day 00:29:42
00:29:42
1 week 00:29:42 …

postgresql hibernate intervals column-types custom-type

13
推荐指数
3
解决办法
1万
查看次数