序列化存储的 json 对象给出空指针 hibernate vladmihalcea / hibernate-types

Cés*_*che 6 serialization json hibernate hibernate-types

我将 java 对象保存为 json 在我的数据库中。对于这个实现,当我保存它时,它工作正常,我可以在我的数据库中找到新行。但是,每当我尝试获取我存储的同一对象时,我都会收到此错误:

java.lang.NullPointerException: null
    at java.lang.Class.isAssignableFrom(Native Method)
    at com.vladmihalcea.hibernate.type.json.internal.JsonTypeDescriptor.fromString(JsonTypeDescriptor.java:104)
    at com.vladmihalcea.hibernate.type.json.internal.JsonTypeDescriptor.wrap(JsonTypeDescriptor.java:165)
    at com.vladmihalcea.hibernate.type.json.internal.AbstractJsonSqlTypeDescriptor$1.doExtract(AbstractJsonSqlTypeDescriptor.java:34)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)...
Run Code Online (Sandbox Code Playgroud)

我的休眠版本是:

Hibernate Core {5.3.7.Final}
Hibernate Commons Annotations {5.0.4.Final}
Run Code Online (Sandbox Code Playgroud)

我正在使用这个库将我的 java 类序列化为 json。

com.vladmihalcea:hibernate-types-52:2.17.3
Run Code Online (Sandbox Code Playgroud)

奇怪的是,我可以将实体保存到数据库(可以反序列化),但当我尝试获取它时,它无法重新构建对象。

@Getter
@Setter
@TypeDefs({
        @TypeDef(name = "json", typeClass = JsonType.class)
})
public class NotificationMessage implements Serializable {
    private Long id;
    private String name = "";
    private String type;
}
@Entity
@Table(name = "notification")
@Getter
@Setter
@EntityListeners(AuditingEntityListener.class)
public class Notification {

    @Id
    @Column(name = "notification_id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_notification_sequence")
    @SequenceGenerator(name = "id_notification_sequence", sequenceName = "id_notification_sequence", allocationSize = 1)
    private Long id;

    @Column(name = "type", nullable = false)
    @Enumerated(EnumType.STRING)
    @NotNull
    private NotificationType type;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_uuid", referencedColumnName = "user_uuid")
    private User user;

    @Type(type = "json")
    @Column(name = "message", columnDefinition = "json")
    @NotNull
    private NotificationMessage message;

    @Column(name = "acknowledged")
    private boolean acknowledged = false;

    @Column(name = "created_date", nullable = false, updatable = false)
    @CreatedDate
    private LocalDateTime createdDate;

    @Column(name = "modified_date")
    @LastModifiedDate
    private LocalDateTime modifiedDate;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Notification that = (Notification) o;
        return id.equals(that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我为获取通知而运行的查询:

@Repository
public interface NotificationRepository extends JpaRepository<Notification, Long>, JpaSpecificationExecutor<Notification> {
    @Query("SELECT n FROM Notification n WHERE n.type = :type AND n.acknowledged = false")
    List<Notification> findAllByTypeAndAcknowledgedIsFalse(@Param("type") NotificationType type);
}
Run Code Online (Sandbox Code Playgroud)

Lou*_*lis 8

您还必须使用 @TypeDef 注释向实体类声明 Hibernate 类型,如下所示:

@Entity
@Table(name = "notification")
@Getter
@Setter
@EntityListeners(AuditingEntityListener.class)
@TypeDef(name = "json", typeClass = JsonType.class)
public class Notification {
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。jsonb 存在同样的问题,已使用 `@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)` 修复 (2认同)