问题
I have a bidirectional relationship. This is my 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
    }
}
This is my entity 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
    }
}
This is my REST controller
@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();
    }
And this is my AngularJS controller:
 $http.get('app/rest/facturas').
                        success(function (data, status, headers, config) {
                            console.log(JSON.stringify(data));
});
Why my collection is null in the AngularJS controller? How can I access to collection?
回答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;
回答2:
In the Factura entity, you need to remove the @JsonIgnore property in the following snippet:
@OneToMany(mappedBy = "factura")
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Facturaservicio> facturaservicios = new HashSet<>();
来源:https://stackoverflow.com/questions/28364711/collection-null-in-angularjs-spring-data-jpa-onetomany-manytoone