簡體   English   中英

無法反序列化異常 spring 引導數據 jpa

[英]Could not deserialize exception spring boot data jpa

我正在使用 postgres 數據庫並使用 spring 數據 jpa 編寫后端代碼。

社區表:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "community_table")
@Entity
public class CommunityTable {
    @Id
    @GeneratedValue
    @Column(name = "community_id")
    private Integer communityId;

    @Column(name = "community_name", unique = true, nullable = false)
    private String communityName;

    @Column(name = "creation_date", nullable = false)
    private Date creationDate;

    @Column(name = "rules", columnDefinition = "text")
    private String[] rules;

    @ManyToOne
    @JoinColumn(name = "creator_id", nullable = false)
    private UserTable creatorId;

    @ManyToOne
    @JoinColumn(name = "current_owner", nullable = false)
    private UserTable currentOwner;
}

用戶表:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "user_table")
@Entity
public class UserTable {
    @Id
    @GeneratedValue
    @Column(name = "user_id", nullable = false)
    private Integer userId;

    @Column(name = "email", nullable = false, unique = true)
    private String email;

    @Column(name = "username", nullable = false, unique = true)
    private String username;

    @Column(name = "join_date", nullable = false)
    private Date joinDate;

    @Column(name = "hashed_password", nullable = false)
    private String hashedPassword;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "last_logged_in", nullable = false)
    private Date lastLoggedIn;

    @OneToMany(mappedBy = "creatorId")
    private List<CommunityTable> creatorCommunity;

    @OneToMany(mappedBy = "currentOwner")
    private List<CommunityTable> ownerCommunity;
}

Controller 代碼失敗:

@GetMapping("/community")
    public ResponseEntity getAllCommunities(){
        try{
            return new ResponseEntity<>(userService.getAllCommunities(), HttpStatus.OK);
        }catch (Exception e){
            log.error(e.toString());
            return new ResponseEntity<String>("Unable to fetch all communities", HttpStatus.CONFLICT);
        }
    }

導致上述 controller 方法失敗的服務代碼:

public List<CommunityTable> getAllCommunities() throws Exception{
        try{
            return communityTableRepository.findAll();
        }catch (Exception e){
            log.error(e.toString());
            throw new Exception("Unable to fetch community due to: "+e.toString());
        }
    }

例外:

Hibernate: select communityt0_.community_id as communit1_0_, communityt0_.community_name as communit2_0_, communityt0_.creation_date as creation3_0_, communityt0_.creator_id as creator_5_0_, communityt0_.current_owner as current_6_0_, communityt0_.rules as rules4_0_ from community_table communityt0_
2022-01-05 22:34:14.989 ERROR 20231 --- [nio-8080-exec-1] c.e.redditbackend.service.UserService    : org.springframework.orm.jpa.JpaSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
2022-01-05 22:34:14.990 ERROR 20231 --- [nio-8080-exec-1] c.e.r.controller.UserController          : java.lang.Exception: Unable to fetch community due to: org.springframework.orm.jpa.JpaSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize

不要介意log.error。 我已經用@Log4j2 注釋了服務和 controller class 所以這不是這里的問題。 由於某種原因,我無法獲取社區表列表。

為什么會出現上述異常的任何想法。

這里的問題是由以下原因引起的:

@Column(name = "rules", columnDefinition = "text")
private String[] rules; // <-- column only holds a single string, not an array!

使用此 columnDefinition,DB 列不包含字符串數組而是單個字符串,因此它只有在這樣定義時才會起作用:

@Column(name = "rules", columnDefinition = "text")
private String rules; // <-- no array!

如果您想要多個(有序)字符串,您可以切換到@ElementCollection

@ElementCollection
@OrderColumn
@Column(columnDefinition = "text")
private List<String> strings; // (<-- not an array, but a list)

或者,如果您確實需要一個數組和/或您想在沒有集合表的情況下將所有值存儲在單個列中,您可以使用hibernate-types

implementation 'com.vladmihalcea:hibernate-types-55:2.14.0'

實體:

@TypeDef(
    name = "string-array", 
    typeClass = StringArrayType.class
)
class MyEntity { // ...

  @Type( type = "string-array" )
  @Column(columnDefinition = "text[]") // <-- column matches entity attribute type
  private String[] strings;

// ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM