6

I'm saving a java object as json in my db. For this implementation when I save it, it works fine and I can find the new row in my db. However whenever I try to fetch the same object I stored, I get this error:

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)...

My hibernate versions are:

Hibernate Core {5.3.7.Final}
Hibernate Commons Annotations {5.0.4.Final}

And I'm using this library to serialize my java class to json.

com.vladmihalcea:hibernate-types-52:2.17.3

What is weird is that I can save entities to the database (can deserialize) but it cannot build the object back when I try to get it.

@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);
    }
}

This is the query I'm running to get the notification back:

@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);
}
6
  • Does this answer your question? What is a NullPointerException, and how do I fix it?
    – Jens
    Commented Aug 12, 2022 at 9:18
  • Are there more informations in the stacktrace?
    – Jens
    Commented Aug 12, 2022 at 9:19
  • full stack trace : textdoc.co/kuysVq3tBTdX48GJ Commented Aug 12, 2022 at 9:26
  • here is the object in db that fails to serialize: 109,a11004d9-bf9d-4c44-8b82-dee551774daa, CREATE,{"id":200,"name":"xxxx","type":"xxxx"},false,2022-08-12 10:44:36.867000,2022-08-12 10:44:36.867000 Commented Aug 12, 2022 at 9:27
  • Which dbms do you use? is the column declared as json in the database?
    – Jens
    Commented Aug 12, 2022 at 9:31

2 Answers 2

8

You must also declare the Hibernate Type to the entity class using the @TypeDef annotation like this:

@Entity
@Table(name = "notification")
@Getter
@Setter
@EntityListeners(AuditingEntityListener.class)
@TypeDef(name = "json", typeClass = JsonType.class)
public class Notification {
1
  • 3
    thanks. same issue here with jsonb, fixed with @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
    – dan carter
    Commented Jan 12, 2023 at 22:46
2

I had the same issue as you, and was stuck on it full day, really frustrating. But now I have fixed it. My fixes are:

  1. Match the versions. I was using Hibernate 5.1 and hypersistence-utils-hibernate-55. However, according to the README in the repo, I should use io.hypersistence: hypersistence-utils-hibernate-5: 3.0.1 with Hibernate 5.1. However, in your case, I would try to unify the Hibernate versions first, and then choose the matching version for hypersistence-utils.
  2. Use the correct annotation. In my Entity, I was annotating the field @Type(type = "jsonb"). After changing it to @Type(type = "json"). It started to work.

Hopefully, this can help you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.