复合模式,我无法从后端检索所有实体

时间:2017-04-18 22:46:16

标签: spring hibernate rest fetch composite

我正在尝试在我的系统中实现和使用复合模式。

问题是我无法从后端检索所有实体层次结构。

我不确定是什么问题,抓取很好。所以,我不确定是否会休眠。

让我们看看,这些是我的实体。

@Entity
@Table(name = "game_rule")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("rule")
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "_class")
@JsonSubTypes({
    @JsonSubTypes.Type(value = SimpleRule.class, name = "SimpleRule"),
    @JsonSubTypes.Type(value = CompositeRule.class, name = "CompositeRule") })
public abstract class GameRule implements Serializable {
private static final long serialVersionUID = -4597791997254248990L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private Long id;
private String operator;

在本课程中,我保存了主要的" GameRule"

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("group")
public class CompositeRule extends GameRule {
private static final long serialVersionUID = 6197786758476721324L;
@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinTable(name = "game_rules_hierarchy",
        joinColumns = @JoinColumn(name = "parent_rule_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "child_rule_id", referencedColumnName = "id"))
@OrderBy("id")
private List<GameRule> rules;

public List<GameRule> getRules() { return rules; }

public void setRules(List<GameRule> rules) { this.rules = rules; }

在本课程中,我保存了GameRules列表

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("simple")
public class SimpleRule extends GameRule {
private static final long serialVersionUID = 6197786758476721324L;

private String variable;
private Double value;

@ManyToOne
@NotNull
private Device device;

现在是叶子实体。

@RequestMapping(value = "/games/{id}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
@Transactional
public ResponseEntity<Game> getGame(@PathVariable Long id) {
   log.debug("REST request to get Game : {}", id);
   Game game = gameRepository.findOne(id);

   return Optional.ofNullable(game)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

现在,restController检索数据

vm.game.gameRule = {id: null, operator: "", type:null, _class:"CompositeRule",
            rules: [ {id: null, operator: "", type:null, _class:"CompositeRule", rules:
                [{id: null, type:null, _class:"SimpleRule", device: "6", variable: "POWER", operator: ">", value: "100"},
                    {id: null, type:null, _class:"SimpleRule", device: "6", variable: "POWER", operator: ">", value: "100"}]}
            ]};

现在在视图中我能够接收CompositeRule对象,但没有关于simpleRule对象的信息。

从UI(角度),我使用此JSON加载层次结构。

vm.game.gameRule = {id: 1, operator: "", type:null, _class:"CompositeRule",
            rules: [ {id: 2, operator: "", type:null, _class:"CompositeRule", rules:[]} ]};]

这是在DB中成功加载的。但问题是当我尝试检索整个层次结构时。目前我只收到CompositeRule对象,例如:

{{1}}

在RestController中,我能够看到所有层次结构对象。我不确定问题是什么。

感谢阅读。

1 个答案:

答案 0 :(得分:0)

我很糟糕!

一切都很好。我的问题出在控制台日志中。似乎chrome中的控制台没有显示所有层次结构。但如果我进入网络视图,我就能看到一切。

感谢。