未能在Spring Boot(JPA/Joda/Jadira)中自动注册类型



我已经研究过了,虽然有很多类似的答案,但我找不到任何答案。

我有一个Spring Boot应用程序(1.3.3.RELEASE),它使用Spring Data JPA和Joda Time;我也使用YAML进行配置。

问题是:我无法使用Jadira自动注册用户类型。

这是我的application.yml文件的相关部分:

  jackson:
    joda-date-time-format: yyyy-MM-dd'T'HH:mm:ss.SSSZ
    serialization:
      write_dates_as_timestamps: false
  jpa:
    properties:
      #jadira_usertype_autoRegisterUserTypes: true
      jadira:
        usertype:
          autoRegisterUserTypes: true

这是JPA的一个实体:

@Entity
@Table(name = "POSTULATIONS")
public final class PostulationEntity implements Serializable {
  private static final long serialVersionUID = 608398264869292985L;
  @Id
  @JsonProperty("id_postulation")
  @Column(name = "ID_POSTULATION") // columnDefinition = "BINARY(16)"
  @GeneratedValue(generator = "uuid2")
  @GenericGenerator(name = "uuid2", strategy = "uuid2")
  private String id;
  @NotNull
  @JsonProperty("issued_at")
  @Column(name = "ISSUED_AT", nullable = false)
  //@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
  private DateTime issuedAt;
  @OneToOne
  @PrimaryKeyJoinColumn
  private PersonEntity person;

我希望能够在不指定@Type的情况下使用DateTime类型。

我已经尝试了几种方法,但如果不在每个DateTime字段上都加上@Type注释,到目前为止都不起作用。

最后,这是我的Gradle配置的相关部分:

  dependencies {
    compile 'com.google.guava:guava:19.0'
    //compile 'org.jadira.usertype:usertype.core:5.0.0.GA' // TODO: ?
    compile 'org.jadira.usertype:usertype.jodatime:2.0.1' // TODO
    compile 'com.fasterxml.jackson.datatype:jackson-datatype-joda' // TODO
  }

提前感谢。。。

根据这篇帖子,我认为你的yaml是错误的:http://blog.jadira.co.uk/blog/2012/1/19/release-jadira-usertype-300cr1-with-support-for-joda-money-0.html

尝试

jpa:
    properties:
      jadira.usertype.autoRegisterUserTypes: true

几个小时后,我通过基于错误的测试找到了答案。除了依赖性之外,一切都很好。控制台上抛出的异常几乎是无用的,尽管通过更改配置设置(和依赖项),它们是不同的,所以这让我有了尝试不同依赖项的想法,因为数据在某些时候被成功导入。

无论如何,application.yml文件保持原样(我删除注释行只是为了避免混淆):

jackson:
  joda-date-time-format: yyyy-MM-dd'T'HH:mm:ss.SSSZ
  serialization:
    write_dates_as_timestamps: false
jpa:
  properties:
    jadira:
      usertype:
        autoRegisterUserTypes: true

所有JPA实体现在都可以在没有@Type注释的情况下使用——至少对于我正在使用的类型是这样。

@Entity
@Table(name = "POSTULATIONS")
public final class PostulationEntity implements Serializable {
  private static final long serialVersionUID = 608398264869292985L;
  ...
  @NotNull
  @JsonProperty("issued_at")
  @Column(name = "ISSUED_AT", nullable = false)
  private DateTime issuedAt;

但是依赖性是这里的关键,也是导致我的问题的因素。首先,我必须去掉org.jadira.usertype:usertype.jodatime,然后放回/激活org.jadira.usertype:usertype.core,但版本4.0.0.GA——因为版本5.0.0.GA不适用于Spring Boot 1.3.3.RELEASE附带的默认依赖项。这是最后一个片段:

dependencies {
  compile 'com.google.guava:guava:19.0'
  compile 'org.jadira.usertype:usertype.core:4.0.0.GA'
  compile 'com.fasterxml.jackson.datatype:jackson-datatype-joda'
}

注意:我没有在其他依赖项上使用版本的原因是我使用的是Spring依赖项管理插件:

apply plugin: 'io.spring.dependency-management'
dependencyManagement {
  imports {
    mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
  }
}
dependencies {
  compile 'com.google.guava:guava:19.0'
  compile 'org.jadira.usertype:usertype.core:4.0.0.GA' // TODO
  compile 'com.fasterxml.jackson.datatype:jackson-datatype-joda' // TODO
}

最新更新