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

wyyhbhjk  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(121)

我用Sping Boot + Hibernate + PostgreSQL做了一个互联网鞋店,有表Goods with information about shoe和Sizes table,其中包含有关具体鞋子尺寸和库存数量的信息。因此,商品表将包含有关其尺寸的信息。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 architectureJSON response w/o info about sizes
我尝试使用复杂的查询hql代码,并使用多个hibernate注解,但没有什么好的发生。。

yyyllmsg

yyyllmsg1#

你在sizes属性中有@JsonBackReference,在Size类中有@JsonManagedReference,进入good属性。你可以用另一个注解来更改一个注解。请参阅此链接:https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

@JsonManagedReference是reference的前向部分,也就是被序列化的部分。
@JsonBackReference为引用的后面部分;序列化时会省略

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

相关问题