提问者:小点点

方法getOne(id)Hibernate SpringMVCJPA Repository上的调用异常


我正在做一个SpringMVC项目,我陷入了这个问题。

我有一个实体“TipoDoc”,他自己的服务和存储库使用JPA存储库,在我的服务中,我有一个getAll()方法,而不是调用findAll()JPA存储库方法,它工作得很好,但是当我想使用一个方法通过id获取一个时,我收到一个空对象,无论我发送给该方法的id是什么。

所以,我开始调试搜索问题,当Hibernate必须从JPA存储库执行方法getOne()时,我发现了一个com. sun.jdi.invocation异常作为响应。

我不知道我的代码中有什么问题,或者如何从异常中获取更多详细信息…我使用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;
    }

持久化. 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>

这就是我能找到的调试

编辑。如果你看不到图像,我可以从异常中获得完整的描述

在目标VM调用方法中出现异常。


共1个答案

匿名用户

该对象不存在于具有给定id的数据库中。显然,您在此处返回的持久性上下文中有该对象的代理。当访问该对象时,它会尝试从数据库中实际加载它,但失败了,因为没有具有该id的行。