簡體   English   中英

jhipster LazyInitializationException

[英]jhipster LazyInitializationException

嗨,我有使用jhipster 4.2.0的微服務應用程序,這是我的Resource類

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個存儲庫,則應通過@Transactional注釋getAllMedidasCliente()將它們包含在同一事務中。

更好的是,引入一種Cliente服務,它將保留事務方面的內容,並且順便說一下,這是yo jhipster:entity提出的選項之一。

另外,您可能只想調用clienteRepository並啟用JPA渴望獲取以使用cliente來獲取所有medidas。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM