Collection AngularJS + Spring 数据 JPA @OneToMany @ManyToOne 中的空值
Collection null in AngularJS + Spring Data JPA @OneToMany @ManyToOne
我有双向关系。
这是我的 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
}
}
这是我的 实体 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));
});
为什么我的 collection 在 AngularJS 控制器中为空?如何访问 collection?
在 Factura 实体中,您需要删除以下代码段中的 @JsonIgnore
属性:
@OneToMany(mappedBy = "factura")
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Facturaservicio> facturaservicios = new HashSet<>();
当 JHipster 创建具有 OneToMany - ManyToOne 关系的实体时,第一个实体 (factura) 具有第二个实体 (facturaservicios) 的列表,但它没有说明关系的类型。
所以解决方案是在@OneToManyRelation中添加fetch = 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;
我有双向关系。 这是我的 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
}
}
这是我的 实体 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));
});
为什么我的 collection 在 AngularJS 控制器中为空?如何访问 collection?
在 Factura 实体中,您需要删除以下代码段中的 @JsonIgnore
属性:
@OneToMany(mappedBy = "factura")
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Facturaservicio> facturaservicios = new HashSet<>();
当 JHipster 创建具有 OneToMany - ManyToOne 关系的实体时,第一个实体 (factura) 具有第二个实体 (facturaservicios) 的列表,但它没有说明关系的类型。
所以解决方案是在@OneToManyRelation中添加fetch = 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;