本文介绍了收集空在AngularJS +弹簧数据的JPA @OneToMany @ManyToOne的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个双向关系。
这是我的实体factura

@Entity
@Table(name = "T_FACTURA")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Factura implements Serializable {
   ...
    @OneToMany(mappedBy = "factura")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Facturaservicio> facturaservicios = new HashSet<>();
    ...
    @Override
    public String toString() {
        //all attributes except facturaservicios
    }
}

这是我的实体facturaservicio

@Entity
@Table(name = "T_FACTURASERVICIO")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Facturaservicio implements Serializable {
    ...
    @ManyToOne
    private Factura factura;
    ...
    @Override
    public String toString() {
        //all attributes except factura
    }
}

这是我的 REST控制器

@RestController
@RequestMapping("/app")
public class FacturaResource {

    private final Logger log = LoggerFactory.getLogger(FacturaResource.class);

    @Inject
    private FacturaRepository facturaRepository;

    @RequestMapping(value = "/rest/facturas",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public List<Factura> getAll() {
        log.debug("REST request to get all Facturas");
        return facturaRepository.findAll();
    }

这是我的 AngularJS控制器

 $http.get('app/rest/facturas').
                        success(function (data, status, headers, config) {
                            console.log(JSON.stringify(data));
});

为什么我的收藏是空的AngularJS控制器?我怎样才能获得集合?

推荐答案

在JHipster创建了一对多一个实体 - 多对一关系使得第一实体(factura)的第二个实体的列表(facturaservicios),但它没有说类型的关系。

When JHipster creates a entity with OneToMany - ManyToOne relationship makes that the first entity (factura) has a list of the second entity (facturaservicios) but it not say the type of relation.

所以,在解决方案是添加的取= FetchType.EAGER 在@OneToManyRelation。

So the solution is add fetch = FetchType.EAGER in the @OneToManyRelation.

@OneToMany(mappedBy = "factura", fetch = FetchType.EAGER)
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Facturaservicio> facturaservicios = new HashSet<>();

@ManyToOne
private Factura factura;

这篇关于收集空在AngularJS +弹簧数据的JPA @OneToMany @ManyToOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 00:07