嗨,我有使用jhipster 4.2.0的微服务应用程序
这是我的资源班
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
/**
* REST controller for managing Medida.
*/
@RestController
@RequestMapping("/api")
public class MedidaResource {
private final Logger log = LoggerFactory.getLogger(MedidaResource.class);
private static final String ENTITY_NAME = "medida";
private final MedidaRepository medidaRepository;
private final ClienteRepository clienteRepository;
public MedidaResource(MedidaRepository medidaRepository, ClienteRepository clienteRepository) {
this.medidaRepository = medidaRepository;
this.clienteRepository = clienteRepository;
}
/**
* POST /medidas : Create a new medida.
*
* @param medida the medida to create
* @return the ResponseEntity with status 201 (Created) and with body the new medida, or with status 400 (Bad Request) if the medida has already an ID
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PostMapping("/medidas")
@Timed
public ResponseEntity<Medida> createMedida(@Valid @RequestBody Medida medida) throws URISyntaxException {
log.debug("REST request to save Medida : {}", medida);
if (medida.getId() != null) {
return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new medida cannot already have an ID")).body(null);
}
Medida result = medidaRepository.save(medida);
return ResponseEntity.created(new URI("/api/medidas/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
.body(result);
}
/**
* PUT /medidas : Updates an existing medida.
*
* @param medida the medida to update
* @return the ResponseEntity with status 200 (OK) and with body the updated medida,
* or with status 400 (Bad Request) if the medida is not valid,
* or with status 500 (Internal Server Error) if the medida couldnt be updated
* @throws URISyntaxException if the Location URI syntax is incorrect
*/
@PutMapping("/medidas")
@Timed
public ResponseEntity<Medida> updateMedida(@Valid @RequestBody Medida medida) throws URISyntaxException {
log.debug("REST request to update Medida : {}", medida);
if (medida.getId() == null) {
return createMedida(medida);
}
Medida result = medidaRepository.save(medida);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, medida.getId().toString()))
.body(result);
}
/**
* GET /medidas : get all the medidas.
*
* @return the ResponseEntity with status 200 (OK) and the list of medidas in body
*/
@GetMapping("/medidas")
@Timed
public List<Medida> getAllMedidas() {
log.info("------------------ REST request to get all Medidas");
List<Medida> medidas = medidaRepository.findAll();
return medidas;
}
/**
* GET /medidas/medidas : get all the medidas por cliente.
*
* @return the ResponseEntity with status 200 (OK) and the list of medidas in body
*/
@GetMapping("/medidas/cliente/{id}")
@Timed
public List<Medida> getAllMedidasCliente(@PathVariable Long id) {
log.debug("REST request to get all Medidas");
log.info("ID de cliente:" + id);
Cliente cliente = clienteRepository.getOne(id);
log.info("Cliente encontrado: "+ cliente);
List<Medida> medidas = medidaRepository.findAllByCliente(cliente);
return medidas;
}
/**
* GET /medidas/:id : get the "id" medida.
*
* @param id the id of the medida to retrieve
* @return the ResponseEntity with status 200 (OK) and with body the medida, or with status 404 (Not Found)
*/
@GetMapping("/medidas/{id}")
@Timed
public ResponseEntity<Medida> getMedida(@PathVariable Long id) {
log.debug("REST request to get Medida : {}", id);
Medida medida = medidaRepository.findOne(id);
return ResponseUtil.wrapOrNotFound(Optional.ofNullable(medida));
}
/**
* DELETE /medidas/:id : delete the "id" medida.
*
* @param id the id of the medida to delete
* @return the ResponseEntity with status 200 (OK)
*/
@DeleteMapping("/medidas/{id}")
@Timed
public ResponseEntity<Void> deleteMedida(@PathVariable Long id) {
log.debug("REST request to delete Medida : {}", id);
medidaRepository.delete(id);
return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
}
}
并且此错误发生在咨询api上:http://localhost:9060/anelapi/api/medidas/cliente/1
2017-04-10 15:48:23.425 DEBUG 10068 --- [ XNIO-2 task-4] c.a.clothes.web.rest.MedidaResource : REST request to get all Medidas
2017-04-10 15:48:23.429 INFO 10068 --- [ XNIO-2 task-4] c.a.clothes.web.rest.MedidaResource : ID de cliente:1
2017-04-10 15:48:23.435 ERROR 10068 --- [ XNIO-2 task-4] c.a.clothes.aop.logging.LoggingAspect : Exception in com.anelsoftware.clothes.web.rest.MedidaResource.getAllMedidasCliente() with cause = 'NULL' and exception = 'could not initialize proxy - no Session'
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:146)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:259)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73)
at com.anelsoftware.clothes.domain.Cliente_$$_jvstc20_0.toString(Cliente_$$_jvstc20_0.java)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.anelsoftware.clothes.web.rest.MedidaResource.getAllMedidasCliente(MedidaResource.java:106)
如果我正在尝试http://localhost:9060/anelapi/api/medidas这项工作还可以
我希望你的帮助,谢谢
最佳答案
如果要使用关系调用2个存储库,则应通过用getAllMedidasCliente()
注释@Transactional
将它们包含在同一事务中。
更好的是,引入将保留事务方面的Cliente服务,并且这是yo jhipster:entity
提出的选项之一。
另外,您可能只想调用clienteRepository并启用JPA渴望获取以使用cliente来获取所有medidas。