如何在Hibernate和JPA中加入uuid列?

时间:2019-05-02 12:40:06

标签: hibernate jpa uuid

我将UUID用作数据库的主键值。 UUID是在Java端生成的,并放置在PostgreSQL数据库中,该数据库的varchar列标记为主键。

我现在正在尝试使用此列在Hibernate中联接两个表。我曾经有int / long数据类型列,并且工作正常。然后切换到UUID并遇到问题。

我的MailDataSource.java类。

@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "mail_datasource_uuid", updatable = false, nullable = false)
private String mailDataSourceUuid;

我的Content.java类,它应采用MailDataSource.java类中生成的UUID,并将其存储在content_datasource_uuid列中。

@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "content_uuid", updatable = false, nullable = false)
private String contentUuid;

@ManyToOne
@JoinColumn(name="content_datasource_uuid")
private Mc360MailDataSource contentDatasourceUuid;

错误:

Field error in object 'content' on field 'contentDatasourceUuid': rejected 
value [2d001537-bf43-4b99-a759-1134da8aa1b8]; codes [typeMismatch.content.contentDatasourceUuid,
typeMismatch.contentDatasourceUuid,typeMismatch.com.data.entity.MailDataSource,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: 
codes [content.contentDatasourceUuid,contentDatasourceUuid]; 
arguments []; default message [contentDatasourceUuid]]; default 
message [Failed to convert property value of type 'java.lang.String' 
to required type 'com.data.entity.MailDataSource' for property 'contentDatasourceUuid'; nested exception is 
org.springframework.core.convert.ConversionFailedException: 
Failed to convert from type [java.lang.String] to type [java.lang.Long] 
for value '2d001537-bf43-4b99-a759-1134da8aa1b8'; nested exception 
is java.lang.NumberFormatException: For input string: 
"2d001537-bf43-4b99-a759-1134da8aa1b8"]

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

问题来自存储库类。

我应该有JpaRepository<MailDataSource, Long>时有JpaRepository<MailDataSource, String>