如何看待JHipster产生的一对多关系中的双方 [英] How to see both sides in one-to-many relationship generated by JHipster

查看:83
本文介绍了如何看待JHipster产生的一对多关系中的双方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用JHipster生成了一个简单的应用程序.我有几个有关系的实体.例如,我有一个 IndexPage ,其中包含几个 IndexAreas .每个 IndexArea 可以包含多个 IndexTiles .每个 IndexTile 都连接到一个 CoursePage .

I've generated a simple app with JHipster. I have several entities which are in relationships. For example, I have an IndexPage which contains several IndexAreas. Each IndexArea can contain several IndexTiles. Each IndexTile is connected to one CoursePage.

默认情况下,我在 @OneToMany 端具有 @JsonIgnore 批注,但这意味着我无法在前端显示所有元素(因为我看不到它们)).例如,我可以编辑 IndexTile 并从下拉列表中选择 IndexArea ,但是我不能通过< IndexArea 中的code> IndexTiles ,因为它们不在JSON中.

By default I had @JsonIgnore annotation at the @OneToMany sides, but that meant I cannot display all the elements in the frontend (because I don't see them). For example, I can edit an IndexTile and pick an IndexArea from the dropdown list, but I can't do ng-repeat through the IndexTiles in an IndexArea, because they are not in the JSON.

如果删除 @JsonIgnore ,则会得到无限递归(这是预期的).因此,我将所有 @JsonIgnore 替换为 @JsonSerialize(使用= MyCustomSerializer.class).这是我目前的状态:

If I remove @JsonIgnore, I would get an infinite recursion (that was expected). So I replaced all the @JsonIgnores with @JsonSerialize(using = MyCustomSerializer.class). Here is the current state that I have:

public class IndexPage {
...
  @OneToMany(mappedBy = "indexPage")
  @JsonSerialize(using = IndexAreaSerializer.class)
  private Set<IndexArea> indexAreas = new HashSet<>();
...
}

public class IndexArea {
...
  @ManyToOne
  private IndexPage indexPage;

  @OneToMany(mappedBy = "indexArea")
  @JsonSerialize(using = IndexTileSerializer.class)
  private Set<IndexTile> indexTiles = new HashSet<>();
...
}

public class IndexTile{
  ...
  @ManyToOne
  private IndexArea indexArea;

  @OneToOne
  @JoinColumn(unique = true)
  private CoursePage coursePage;
  ...
}

public class CoursePage {
  ...
  @OneToOne(mappedBy = "coursePage")
  @JsonIgnore // a CoursePage doesn't care about the indexTile
  private IndexTile indexTile;
  ...
}

现在,当我刷新页面时,出现错误消息:

Now when I refresh my page, I get an error:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: rs.kursnemackog.domain.IndexPage.indexAreas, could not initia
lize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: rs.kursnemackog.domain.IndexPage.indexAreas, could no
t initialize proxy - no Session (through reference chain: rs.kursnemackog.domain.IndexPage["indexAreas"])

我该怎么做才能看到关系的两侧并正常使用它们(例如,能够为 IndexTile 选择一个 IndexArea 还要通过某个 IndexArea 中的所有 IndexTiles 进行 ng-repeat )?

What can I do in order to be able to see both sides of the relation and to use them normally (e.g. to be able to pick an IndexArea for an IndexTile and also to ng-repeat through all the IndexTiles in a certain IndexArea)?

谢谢.

推荐答案

延迟加载异常是正常的,因为JHipster将所有关系都声明为延迟加载,这被认为是一种好习惯.例外是由于缺少会话,因为它是在JSON序列化时完成的,因此在服务或存储库层中关闭事务之后.

Lazy loading exception is normal because JHipster declares all relationships as lazy loaded which is considered as a good practice. The exception is due to lack of session because it is done as the time of the JSON serialization so after your transaction has been closed in service or repository layer.

有几种解决方案,例如使用 @Transactional open-session-in-view-属性扩展交易范围,但通常最好修改存储库以求取您可以使用 @EntityGraph 或查询语言来建立您的关系.

There are several solutions like extending the scope of transaction with @Transactional or open-session-in-view property but it's usually better to modify your repository to eager fetch your relationship either using @EntityGraph or in query language.

此处提供更多提示: FetchMode如何在Spring中工作数据JPA

此外,您可能希望在JHipster中使用DTO和Service类选项,以避免在REST API中公开您的实体,并获得对Angular应用程序消耗的对象的更多控制.

Additionally, you might want to use DTOs and Service class options in JHipster to avoid exposing your entities in your REST API and get more control about the objects consumed by your Angular app.

这篇关于如何看待JHipster产生的一对多关系中的双方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆