Search code examples
javaspringspring-boothibernatejpa

JPA: Could not determine recommended JdbcType for <SomeCustomClass>


I've written this @Entity in Java 17 using Spring Boot 3 to store information about quotes in a corresponding MySQL table.

package ch.pcngroup.gestionale.entity;

import java.math.BigDecimal;
import java.util.Currency;
import java.util.List;

import ch.pcngroup.gestionale.financial.Item;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(makeFinal = false, level = AccessLevel.PRIVATE)
@Getter
@NotNull
public class Quote {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    Long id;
    
    @OneToOne
    @JoinColumn(referencedColumnName="id")
    PrivateCustomer customer;
    
    Currency currency;
    @Column(columnDefinition="JSON")
    List<Item> items;
}

Now, the items attribute is of type List<Item>, where Item is another class that I've written. Item should not be an @Entity, because I don't want a separate table for Items. The only thing I want to do with Items, is to store a list of them in JSON format in the items field of the quotes table / Object.

Here is Item.java:

package ch.pcngroup.gestionale.financial;

import java.math.BigDecimal;

import lombok.Value;

@Value
public class Item {

    String name;
    String supplierName;
    int quantity;
    BigDecimal price;
    
}

The problem is, that when I run the application I get following exception: org.hibernate.type.descriptor.java.spi.JdbcTypeRecommendationException: Could not determine recommended JdbcType for ch.pcngroup.gestionale.financial.Item.

I can't seem to find documentation or a similar case online.

May one of you know how can I solve this?

Thank you.

I tried some annotation like @Column(columnDefinition="JSON") but it didn't help.


Solution

  • The current solution for that is actually to use @JdbcTypeCode(SqlTypes.JSON).

    For example:

    @Entity
    public class Quote {
        
        ...
    
        @JdbcTypeCode(SqlTypes.JSON)
        @Column(columnDefinition="JSON")
        List<Item> items;
    
        ...
    }