首页 > 解决方案 > 进行 Hibernate 映射时,整个表未在 JSON 中显示

问题描述

该表已成功创建并填充了 H2 数据库中的信息,如下所示:

当使用 Spring boot 以 JSON 格式显示此表信息时,我只看到:

在这里您可以看到来自对象的代码片段

package com.share.sharelt.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Data;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

@Entity
@Data
@Table(name = "item_rental")
public class ItemRental {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    @Column(name = "created")
    private Date created;

    @Column(name = "cost")
    BigDecimal cost;

    @Column(name = "rent_begin")
    private Date rentBegin;

    @Column(name = "rent_end")
    private Date rentEnd;

    @Column(name = "is_confirmed")
    private boolean isConfirmed;

    @JsonBackReference
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "renter_id", nullable = true)
    private User user;

    public ItemRental(){};

}

问题是我想查看整个表信息,更具体地说是“renter_id”列

标签: javajsonspringhibernatemapping

解决方案


解决方案之一是创建一个DTO类,该类将成为您的实体的JSON包装器ItemRental

ItemRentalDTO与 实体类UserDTO 的所有字段类似ItemRental的东西User

链接:https ://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application


推荐阅读