Search code examples
javahibernatespring-datadaolombok

Boolean field in entity changes name in JSON


Something curious is happening in my application and I would like to know the reason.

I have a resource that is constructed with a backend service in my Spring server, and when the JSON arrives at the front end one of the property names is different. I have followed with breakpoints the entire construction of the resource and at no point before returning the query does the property name ever change from isHiddenOnQuote - as you probably suspect, it's defined as a boolean in the object model. My database stores the value as a 1 or 0.

When the JSON is received by my front end, the property name changes to hiddenOnQuote - the "is" magically drops off. Strange as well, is the fact that I have other boolean fields in the JSON that don't change; they retain their "is"ness.

Here is a snippet from the model. Note that none of these properties exist in the superclass, BaseEntity.

package com.company.app.model.sales;

import com.company.app.model.BaseEntity;
import lombok.Data;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.List;

@Data
@Entity
@SQLDelete(sql =
        "UPDATE product_option_category " +
                "SET is_deleted = true " +
                "WHERE id = ?")
@Where(clause = "is_deleted = false")

public class ProductOptionCategory extends BaseEntity {
    private String categoryName;

    private int optionLimit;

    private int mnSegment;

    private boolean isBitwise;

    private boolean areOptionsRepeatable = false;

    private boolean isHiddenOnQuote = false;

    public boolean getIsBitwise() {
        return isBitwise;
    }
}

Is this a Lombok thing?


Solution

  • Seems like you may have hit upon a bug if isBitwise works well but not isHiddenOnQuote.

    Also, noticed that you are not using the @Getter etc annotations from Lombok. Perhaps you can try using the following annotations to force the correct name:

    @get:JsonProperty("isHiddenOnQuote")

    @param:JsonProperty("isHiddenOnQuote")

    I got this from this answer on Stackoverflow: https://stackoverflow.com/a/55100741/4402505

    EDIT: Fixed the property name.