Hibernate @JoinColumn并获取单列而不是模型对象

时间:2018-03-05 09:48:58

标签: java json hibernate hibernate-mapping hibernate-criteria

Iam使用hibernate实体类并与另一个模型具有ManyToOne关系,如下所示

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

我的模态如下

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

}

因此,在获取数据时,我将连接列作为单独的json对象,但我只需要来自该连接模型的单个列。

我得到的结果如下

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

但我希望如此

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

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

您必须使用注释@JsonIgnore注释字段品牌。 比你不会在json回复中收到它