首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用与SQL兼容的列类型在Spring中存储Joda时间(任意)?

如何使用与SQL兼容的列类型在Spring中存储Joda时间(任意)?
EN

Stack Overflow用户
提问于 2015-12-24 14:45:51
回答 1查看 1.5K关注 0票数 7

我正在学习如何使用Spring访问数据库。

我编写了一个包含速溶字段的实体应用程序:

代码语言:javascript
复制
@Entity
@Table(
   name = "MEAL",
   uniqueConstraints=@UniqueConstraint(columnNames = {"USER_ID", "TIMESTAMP_FIELD"}))
public class Meal {

   //private static DateTimeZone defaultTimeZone = DateTimeZone.UTC;

   @Id
   @GeneratedValue(strategy= GenerationType.AUTO)
   @Column(name = "ID")
   private long id;

   @ManyToOne//(fetch=FetchType.LAZY)
   @JoinColumn(name="USER_ID")
   private User user;

   // @Temporal(TemporalType.TIMESTAMP) if non-Joda time
   //@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") 
   @Column(name = "TIMESTAMP_FIELD")
   private Instant timestampField;

我无法用@Temporal@Type注释编译/运行它们,因此它们被注释掉了。

此外,我无法在application.properties中设置application.properties时运行。

它造成了一个错误

代码语言:javascript
复制
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.registerTypeOverride(Lorg/hibernate/usertype/UserType;[Ljava/lang/String;)Lorg/hibernate/cfg/Configuration;

因此,这一行也被注释掉了。

代码语言:javascript
复制
spring.datasource.url=jdbc:h2:tcp://localhost/~/pdk
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

# Jadira requirement
#spring.jpa.properties.jadira.usertype.autoRegisterUserTypes=true

不幸的是,在最后的状态下,我得到了一些字段内容中的序列化/二进制数据。

这里是否可能有一些与SQL兼容的列类型?

更新

我发现,以下注释运行良好:

代码语言:javascript
复制
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentInstantAsTimestamp")

但是为什么在application.properties中设置不起作用呢?

EN

回答 1

Stack Overflow用户

发布于 2016-01-12 13:55:57

偶然发现了同样的问题,结果发现Jadira的版本(5.0.0.GA)与Hibernate 4.xSPI不兼容。

切换到4.0.GA完成了这个任务,并且使用jadira.usertype.autoRegisterUserTypes=true注册Jadira的类型开始工作。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34454453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档