如何使用 Hibernate @ManyToOne 关系在 JSON 响应中显示源表中的数据?

问题描述 投票:0回答:1

我用 Spring Boot + Hibernate + PostgreSQL 创建了互联网鞋店,并且有表格 Goods 包含关于鞋子的信息和 Sizes 表,其中包含关于具体鞋码和库存数量的信息。因此,带有商品的表格将包含有关其尺寸的信息。 Table Sizes 包含 Goods 中 Id 的外键,我希望 Hibernate 解析将包含大小的 JSON

来自好实体的关系

@Entity
@Table(name = "goods")
@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "id")
public class Good {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int Id;

    @Column(name = "Title")
    private String title;

    @Enumerated(EnumType.STRING)
    @Column(name = "sex")
    private sexType sexType;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.DETACH,
    CascadeType.REFRESH, CascadeType.MERGE})
    @JoinColumn(name = "ManufacturerId")
    @JsonManagedReference //-------------------------------------------------------
    Manufacturer manufacturer; //Many goods can refer to one manufacturer

    @JsonBackReference
    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.DETACH,
            CascadeType.REFRESH, CascadeType.MERGE}, mappedBy = "good")  //Uni-directional reference to size array
            //@JoinColumn(name= "goodid")
    List<Size> sizes; //Refactor to bi-directional relationship

    public void addSizeToGood(Size size) {
        if (sizes == null) sizes = new ArrayList<>();
        sizes.add(size);
        //size.setGood(this);
    }

尺寸实体

@Entity
@Table(name = "sizes")
@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "id")
public class Size {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int Id;

    @Column(name = "Size")
    private int size;

    @Column(name = "Quantity")
    private int quantity;

    @Column(name = "goodid")
    private int goodId;

    @JsonManagedReference
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "goodId")
    Good good;

返回商品列表的 DAO 方法

@Repository
public class GoodDAOImpl implements GoodDAO{

    @Autowired
    private EntityManager entityManager;

    @Override
    @Transactional //Spring open and close transactions automatically
    public List<Good> getAllGoods() {
        Session session = entityManager.unwrap(Session.class);

        //Query<Good> query = session.createQuery("from Good", Good.class);
        Query<Good> query = (Query<Good>) entityManager.createQuery("from Good right join Size on Good.");
        List<Good> allGoods = query.getResultList();

        return allGoods;
    }
}

DB architecture JSON response w/o info about sizes

我尝试使用复杂的查询 hql 代码并使用多个 hibernate 注释但没有任何好事发生......

java spring spring-boot hibernate jpa
1个回答
1
投票

您在

@JsonBackReference
属性中具有
sizes
,在Size类中具有
@JsonManagedReference
,成为良好的属性。您可以用一个注解更改另一个注解。请参阅此链接:https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

@JsonManagedReference 是引用的前向部分,gets serialized 正常。

@JsonBackReference 是引用的后面部分; 连载会省略.

如果您在某些情况下需要显示 Size 对象及其良好的相关属性,您可以创建一个 SizeDto 并且可以在其中包含不同的注释。

© www.soinside.com 2019 - 2024. All rights reserved.