Dim*_*ims 7 java sql spring jpa jodatime
我正在学习如何使用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;
Run Code Online (Sandbox Code Playgroud)
我无法编译/运行@Temporal或@Type注释,所以他们被注释掉了.
另外,我无法使用spring.jpa.properties.jadira.usertype.autoRegisterUserTypesin 的设置运行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;
Run Code Online (Sandbox Code Playgroud)
所以,这一行也被注释掉了.
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
Run Code Online (Sandbox Code Playgroud)
不幸的是,在最终状态下,我在字段内容中获得了一些序列化/二进制数据
这里有可能有一些SQL兼容的列类型吗?
UPDATE
我发现,以下注释工作正常:
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentInstantAsTimestamp")
Run Code Online (Sandbox Code Playgroud)
但为什么设置application.properties不起作用?
偶然发现了同样的问题,结果发现我使用的 Jadira usertype.core 版本 (5.0.0.GA) 与 Hibernate 4.x SPI 二进制不兼容。
切换到 4.0.0.GA 就成功了,并且使用注册 Jadira 的类型jadira.usertype.autoRegisterUserTypes=true开始工作。
| 归档时间: |
|
| 查看次数: |
1294 次 |
| 最近记录: |