我一直在用 JPA 在 Spring Boot Web 中撰寫一個程式,并且我正在使用查詢來訪問一些帶有“包含”和“忽略大小寫”過濾器的資料,我之前在其他程式中做過這個,它運行良好,但現在我遇到了這個錯誤,我現在完全迷失了,因為我在谷歌中找不到任何東西,我在兔子洞里走得很遠,想知道它為什么會發生,到目前為止我還沒有看到我的代碼中有任何不合適的地方,宣告的變數型別似乎沒問題,但正如我所說,我迷路了。重要的是要提到,由于某種原因,當我第一次在我的網站上進行查詢時,一切正常,我得到了正確的結果,但是當我回到家并嘗試另一個查詢(甚至是相同的) 我得到了錯誤。下面的代碼:
模型
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Serie {
@Id
@Column(columnDefinition = "NUMERIC(19,0)")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
private String red;
@Column(columnDefinition = "NUMERIC(19,0)")
private double rating;
存盤庫
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import cl.desafiolatam.imdb.modelo.Serie;
public interface SerieRepository extends JpaRepository<Serie, Integer> {
public List<Serie> findByTitleContainingIgnoreCase(String title);
}
服務
import cl.desafiolatam.imdb.vo.SerieVO;
public interface SerieService {
public SerieVO findByTitleContainingIgnoreCase(String title);
}
Service implementation
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cl.desafiolatam.imdb.dao.SerieRepository;
import cl.desafiolatam.imdb.modelo.Serie;
import cl.desafiolatam.imdb.service.SerieService;
import cl.desafiolatam.imdb.vo.SerieVO;
@Service
public class SerieServiceImpl implements SerieService {
private static final Logger logger = LoggerFactory.getLogger(SerieServiceImpl.class);
@Autowired
SerieRepository dao;
SerieVO respuesta;
@Override
@Transactional(readOnly = true)
public SerieVO findByTitleContainingIgnoreCase(String title) {
respuesta = new SerieVO("Ha ocurrido un error!", "104", new ArrayList<Serie>());
try {
List<Serie> serie = dao.findByTitleContainingIgnoreCase(title);
System.out.println(serie);
if(serie.size() > 0) {
respuesta.setSeries(serie);
respuesta.setMensaje("Se ha encontrado el registro");
respuesta.setCodigo("0");
} else {
respuesta.setMensaje("No se ha encontrado el registro");
respuesta.setCodigo("104");
}
} catch (Exception e) {
logger.error("Error al buscar la serie", e);
}
return respuesta;
}
}
Visual object
import java.util.List;
import cl.desafiolatam.imdb.modelo.Serie;
public class SerieVO extends GenericVO {
List<Serie> series;
public SerieVO(String mensaje, String codigo, List<Serie> series) {
super(mensaje, codigo);
this.series = series;
}
public SerieVO() {
super();
}
Controller
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import cl.desafiolatam.imdb.modelo.Serie;
import cl.desafiolatam.imdb.service.SerieService;
import cl.desafiolatam.imdb.vo.SerieVO;
@Controller
public class SerieController {
private final static Logger logger = LoggerFactory.getLogger(SerieController.class);
@Autowired
private SerieService svc;
@GetMapping("/buscarSerie")
public ModelAndView buscarSerie(Model model, @RequestParam String nombreSerie) {
SerieVO respuestaServicio = new SerieVO();
respuestaServicio.setMensaje("No se ha encontrado la serie");
try {
respuestaServicio = svc.findByTitleContainingIgnoreCase(nombreSerie);
model.addAttribute("listaSeries", respuestaServicio.getSeries());
return new ModelAndView("resultadoserie");
} catch (Exception e) {
logger.error("Error al buscar la serie", e);
}
return new ModelAndView("redirect:/user");
}
}
Search input
<div class="d-flex justify-content-center pb-2">
<div class="container row">
<div class="col-md-4">
<div class="d-flex justify-content-center">
<h2>Buscar serie</h2>
</div>
</div>
<div class="col-md-8">
<form action="buscarSerie" method="get">
<div class="row g-2">
<div class="col-md">
<div class="form-floating">
<input type="text" class="form-control" id="floatingInputGrid"
value="" name="nombreSerie" required> <label
for="floatingInputGrid">Serie</label>
</div>
</div>
</div>
<div class="d-flex justify-content-center pt-4">
<input type="submit" class="btn m-2 btn-dark" value="Buscar" />
</div>
</form>
</div>
</div>
</div>
As i've said, im really lost, researched everywhere, and checked the code in my last projects, i just can't find out why this one does me this dirty. Won't even fail at the start, it gives me a glimpse of hope and when i want to retry it, it crushes that little hope. :)
I tried deleting my code and copy&paste from projects where i know it works as intended, changed the variable and param. names to make it work with the new program but didn't work. Did a side by side comparison, tried a @Query writing the specific instruction. Looking for info. only with the 'contains' filter and yet, nothing worked.
uj5u.com熱心網友回復:
根據 Spring Data JPA issue #2472,這似乎是 Hibernate 5.6.6 和 5.6.7 中的一個問題。
休眠錯誤是HHH-15142。
解決方案是降級到 Hibernate 5.6.5 或等待 Hibernate 補丁解決此問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/451114.html
標籤:java spring spring-boot jsp jpa
