1

Iam using a hibernate entity class and have a ManyToOne relation with another model as follows

@ManyToOne
    @JoinColumn(name ="`brand-id`", referencedColumnName="`id`", nullable=false, insertable = false, updatable = false)
    private HotelBrand brand;

and my modal is as follows

@Entity
@Table(name = "`hotel`" })
public class Hotel implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="`id`", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotEmpty
    @Column(name="`brand-id`", nullable=false)
    private Integer brandid;

    @NotEmpty
    @Column(name="`hotel-code`", nullable=false)
    private String hotelid;

    @NotEmpty
    @Column(name="`hotel-name`", nullable=false)
    private String hotelname;

    @NotEmpty
    @Column(name="`have-reports`", nullable=false)
    private String havereports;

    @ManyToOne
    @JoinColumn(name ="`brand-id`", referencedColumnName="`id`", nullable=false, insertable = false, updatable = false)
    private HotelBrand brand;

    public Hotel() {}

    public Hotel(Integer id, Integer brandid, String hotelid, String hotelname, HotelBrand brand ) {
        super();
        this.id = id;
        this.brandid = brandid;
        this.hotelid = hotelid;
        this.hotelname = hotelname;
        this.brand = brand;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getBrandid() {
        return brandid;
    }

    public void setBrandid(Integer brandid) {
        this.brandid = brandid;
    }

    public String getHotelid() {
        return hotelid;
    }

    public void setHotelid(String hotelid) {
        this.hotelid = hotelid;
    }

    public HotelBrand getBrand() {
        return brand;
    }

    public void setBrand(HotelBrand brand) {
        this.brand = brand;
    }

    public String getHotelname() {
        return hotelname;
    }

    public void setHotelname(String hotelname) {
        this.hotelname = hotelname;
    }

}

so when fetching data iam getting joined column as seperate json object but i need only single column from that joined model.

Iam getting result as follows

{
    "id": 115,
    "brandid": 7,
    "hotelid": "ABC",
    "hotelname": "sample1",
    "brand": {
        "id": 7,
        "brandname": "brand1"
    }
}

But iam expecting as

{
    "id": 115,
    "brandid": 7,
    "hotelid": "ABC",
    "hotelname": "sample1",
    "brandname": "brand1"
}

any help would be much appreciated.

Exact Finder
  • 73
  • 2
  • 12

1 Answers1

0

You have to annotate the field brand with the annotation @JsonIgnore. Than you will not received it in the json response

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
muboch
  • 1
  • 1
  • 3