2015-02-06 59 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