首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >序列化存储的json对象提供空指针hibernate vladmihalcea /hibernate-类型。

序列化存储的json对象提供空指针hibernate vladmihalcea /hibernate-类型。
EN

Stack Overflow用户
提问于 2022-08-12 09:16:42
回答 2查看 536关注 0票数 1

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

代码语言:javascript
运行
复制
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)...

我的hibernate版本是:

代码语言:javascript
运行
复制
Hibernate Core {5.3.7.Final}
Hibernate Commons Annotations {5.0.4.Final}

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

代码语言:javascript
运行
复制
com.vladmihalcea:hibernate-types-52:2.17.3

奇怪的是,我可以将实体保存到数据库中(可以反序列化),但当我试图获取对象时,它不能构建回对象。

代码语言:javascript
运行
复制
@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);
    }
}

这是我正在运行的获取通知的查询:

代码语言:javascript
运行
复制
@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);
}
EN

回答 2

Stack Overflow用户

发布于 2022-09-23 13:00:26

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

代码语言:javascript
运行
复制
@Entity
@Table(name = "notification")
@Getter
@Setter
@EntityListeners(AuditingEntityListener.class)
@TypeDef(name = "json", typeClass = JsonType.class)
public class Notification {
票数 1
EN

Stack Overflow用户

发布于 2022-11-20 12:31:23

我和你有同样的问题,整天忙着,真是令人沮丧。但现在我已经修好了。我的解决办法是:

  1. 与版本匹配。我使用Hibernate 5.1和vladmihalcea hibernate-类型-55。但是,根据回购(https://github.com/vladmihalcea/hibernate-types)中的自述文件,我应该使用com.vladmihalcea: Hibernate-types 5: 2.20.0Hibernate 5.1。但是,在您的情况下,我将尝试首先统一Hibernate版本,然后为hibernate-types.
  2. Use选择匹配的版本--正确的注释。在我的实体中,我正在注释字段@Type(type = "jsonb")。将其更改为@Type(type = "json")之后。它开始起作用了。

希望这能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73331882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档