AngularJS + Spring Data JPA @OneToMany @ManyToOne中的集合null

时间:2015-02-06 11:25:23

标签: angularjs spring jpa jhipster

我有双向关系。 这是我的 entity 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
    }
}

这是我的 entity facturaicicio

@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控制器中为空?我如何访问收藏?

2 个答案:

答案 0 :(得分:1)

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.

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;

答案 1 :(得分:0)

在Factura实体中,您需要删除以下代码段中的@JsonIgnore属性:

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