尝试将UUID属性(外键)转换为实体对象(已解决)的异常

时间:2019-06-18 04:42:19

标签: java hibernate spring-boot spring-data-jpa

我正在开发一个spring-boot应用程序(jpa ...等),但遇到了一个问题。我正在使用UUID属性作为主键。当我尝试将对象创建为外键时,JPA无法正确投射到我的对象上。

解决方案是

我的UserEntity

@Entity(name = "User")
@Table(name = "USR")
@EntityListeners(UserPersistListener.class)
@EqualsAndHashCode
public class UserEntity {

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(name = "ID", updatable = false)
    @Getter
    @Setter
    private UUID id;

    @Column(name = "LOGIN", updatable = false)
    @Getter
    @Setter
    private String login;

    @Column(name = "PASS")
    @Getter
    @Setter
    private String pass;

    @Enumerated(EnumType.STRING)
    @Column(name = "ROLE")
    @Getter
    @Setter
    private Role role;

}

使用UserEntity(外键)的其他实体人

@Entity(name = "Person")
@Table(name = "PERSON")
@EqualsAndHashCode
public class PersonEntity {

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(name = "ID", updatable = false)
    @Getter
    @Setter
    private UUID id;

    @Column(name = "NAME")
    @Getter
    @Setter
    private String name;

    @Column(name = "SUBSCRIPTION")
    @Getter
    @Setter
    private Long subscription;

    @Enumerated(EnumType.STRING)
    @Column(name = "SUBSCRIPTION_TYPE")
    @Getter
    @Setter
    private SubscriptionType subscriptionType;

    @Column(name = "PHONE1")
    @Getter
    @Setter
    private Long phone1;

    @Column(name = "PHONE2")
    @Getter
    @Setter
    private Long phone2;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CREATED_BY", referencedColumnName = "ID", updatable = false)
    @Getter
    @Setter
    private UserEntity createdBy;

    @Convert(converter = LocalDateAttributeConverter.class)
    @Column(name = "CREATION_DATE")
    @Getter
    @Setter
    private LocalDateTime creationDate;

}

异常

org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0]->br.com.orangesun.entity.person.PersonEntity["createdBy"]->br.com.orangesun.entity.user.UserEntity_$$_jvst424_1["id"])
2019-06-18 00:52:55.163  WARN 15432 --- [nio-8099-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42883
2019-06-18 00:52:55.164 ERROR 15432 --- [nio-8099-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: operator does not exist: uuid = character varying
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.

编辑

我的数据库定义

| Field Name        | Type    |
|-----------------------------|
| id                | uuid    |
| name              | varchar |
| subscription      | int8    |
| subscription_type | varchar |
| created_by        | uuid    |
| creation_date     | instant |
| phone1            | int8    |
| phone2            | int8    |
|-----------------------------|

编辑2

有关同一错误的其他详细信息

java.lang.IllegalArgumentException: Provided id of the wrong type for class br.com.orangesun.entity.person.PersonEntity. Expected: class java.lang.String, got class java.util.UUID

3 个答案:

答案 0 :(得分:2)

尝试对“ id”字段使用UUID类型而不是字符串,因为UUID是二进制格式,而不是基于字符,因此数据库对UUID字段使用特殊类型。

答案 1 :(得分:0)

如下更新您的PersonEntity类,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CREATED_BY", referencedColumnName = "ID”, updatable = false)
@Getter
@Setter
private UserEntity createdBy;

将referencedColumnName添加到您的@joinColumn

答案 2 :(得分:0)

Postgres中的

UUID会自动为您转换为UUID数据类型。您必须将id数据类型从String更改为UUID,一切都会按预期进行。

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(name = "ID", updatable = false)
@Getter
@Setter
private UUID id;