【发布时间】:2021-03-07 11:05:01
【问题描述】:
我正在做一个 Spring MVC 项目并陷入了这个问题。
我有一个实体“TipoDoc”,他自己的服务和使用 JPA 存储库的存储库,在我的服务中我有一个 getAll() 方法而不是调用 findAll() JPA 存储库方法,它工作正常,但是当我想使用一种通过 id 获取一个的方法,无论我发送给该方法的 id 是什么,我都会收到一个 null 对象。
所以,我开始调试搜索问题,当 Hibernate 必须从 JPA Repository 执行方法 getOne() 时,我发现了一个 com.sun.jdi.invocationexception 作为响应。
我不知道我的代码有什么问题,或者如何从异常中获取更多详细信息。我使用 log4j 进行日志记录,但我不知道如何在日志中捕获该异常。
我正在使用 MySql 数据库
这是我的代码
@Entity
@Table(name = "TiposDocumento")
public class TipoDoc
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idTipoDocumento")
private long id;
private String descripcion;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
}
服务
@Service
public class TipoDocService {
private final TipoDocRepo tipoDocRepo;
@Autowired
public TipoDocService(TipoDocRepo tipoDocRepo) {
this.tipoDocRepo = tipoDocRepo;
}
public List<TipoDoc> getAll() {
return (List<TipoDoc>)tipoDocRepo.findAll();
}
public TipoDoc getById(Long id) {
return (TipoDoc) tipoDocRepo.getOne(id);
}
}
存储库
public interface TipoDocRepo extends JpaRepository<TipoDoc, Long>{
}
控制器
@Controller
public class ClientController
{
private static final Logger logger = Logger.getLogger(ClientController.class);
private final ClienteService clienteService;
private final TipoDocService tipoDocService;
private final EstadoCivilService estadoCivilService;
private final ProvinciaService provinciaService;
private final LocalidadService localidadService;
private final CondIvaService condIvaService;
@Autowired
public ClientController(ClienteService clienteService, TipoDocService tipoDocService, EstadoCivilService estadoCivilService,
ProvinciaService provinciaService, LocalidadService localidadService, CondIvaService condIvaService) {
this.clienteService = clienteService;
this.tipoDocService = tipoDocService;
this.estadoCivilService = estadoCivilService;
this.provinciaService = provinciaService;
this.localidadService = localidadService;
this.condIvaService = condIvaService;
}
@RequestMapping("/Clientes")
public ModelAndView formularioCliente()
{
ModelAndView mav = new ModelAndView("clientes");
mav.getModel().put("cliente",new Cliente());
mav.getModel().put("tiposDoc", tipoDocService.getAll()); //Works fine, tiposDoc={{1,DNI};{2,Passaport};{3,LC}}
TipoDoc tipoDoc = tipoDocService.getById((long) 1); //not working tipoDoc={0,null} when it have to be {1,DNI}
mav.getModel().put("estadosCiviles", estadoCivilService.getAll());
mav.getModel().put("provincias", provinciaService.getAll());
mav.getModel().put("localidades", localidadService.getAll());
mav.getModel().put("condicionesIva", condIvaService.getAll());
return mav;
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="OFYS">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="admin" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/OFYS" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
</properties>
</persistence-unit>
</persistence>
And this is what can I find debuging
编辑。 如果您看不到图像,这是我可以得到的异常的完整描述
com.sun.jdi.invocationexception: 目标虚拟机调用方法发生异常。
【问题讨论】:
标签: java mysql hibernate spring-mvc jpa