2015-02-06 58 views
0

我有雙向關係。 這是我實體factura收集null在AngularJS + Spring數據JPA @OneToMany @ManyToOne

@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(); 
    } 

這是我安固larJS控制器

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

爲什麼我的收藏是在AngularJS控制器空?我如何訪問收藏?

+0

您在您的$ http.get中使用相對URL,是否正確?你確定肯定有數據要返回嗎?什麼是狀態碼? – thedoctor 2015-02-06 11:55:36

+0

@thedoctor我使用郵遞員,它返回了一個JSON,除了我的集合之外的所有屬性。如果我用console.log()打印,它返回null。我沒有任何錯誤 – 2015-02-06 12:22:28

+0

您是否嘗試在您的get方法中添加@ResponseBody註釋? – thedoctor 2015-02-06 12:29:04

回答

2

當JHipster創建實體一對多 - 多對一關係使得第一實體(factura)的列表的第二個實體(facturaservicios),但它沒有說關係的類型。

所以溶液處於@OneToManyRelation添加取= FetchType.EAGER

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

@ManyToOne 
private Factura factura; 
+1

截至評論發佈時,您的答案包括「@ JsonIgnore」註釋。你確定你不需要刪除這個註釋嗎? – Abdull 2017-03-08 10:12:30

0

在Factura的實體,您需要刪除下面的代碼片段的@JsonIgnore屬性:

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

如果我刪除@JsonIgnore,它將打印出「facturaservicios」:null – 2015-02-09 15:32:45