我正在学习如何使用Spring Boot JPA访问数据库.
我用一些实体编写了一个应用程序,其中包含Instant字段:
@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
注释,所以他们被注释掉了.
另外,我无法使用spring.jpa.properties.jadira.usertype.autoRegisterUserTypes
in 的设置运行application.properties
这导致了错误
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;
所以,这一行也被注释掉了.
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兼容的列类型吗?
UPDATE
我发现,以下注释工作正常:
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentInstantAsTimestamp")
但为什么设置application.properties
不起作用?