有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SpringDataJPA查询返回空的EmbeddedId

我有一个带有嵌入Id的类。当我尝试使用Jpa存储库进行搜索时,它只返回一个空对象。看起来问题出在嵌入式Id中,因为我用一个没有这个的类做了一个测试,结果很好

当我对JPA输出的查询而不是数据库进行测试时,它在控制台中运行良好

并且没有输出错误

编辑:数据库中有数据

EDIT2:添加了equals和hashcode

编辑3:findAll方法有效

实体

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.joda.time.DateTime;

@Entity
@Table(name="C_INFO_CADASTRO")
public class Cliente implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ClienteId id;

    @Column(name="DAT_NASC")
    private DateTime dataNascimento;

    @Column(name="TXT_EMAIL")
    private String email;

    @Column(name="NOM_CLIENTE")
    private String nomeCliente;

    @Column(name="NUM_CPF")
    private Long numeroCpf;

    public ClienteId getId() {
        return id;
    }
    public void setId(ClienteId id) {
        this.id = id;
    }
    public DateTime getDataNascimento() {
        return dataNascimento;
    }
    public void setDataNascimento(DateTime dataNascimento) {
        this.dataNascimento = dataNascimento;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getNomeCliente() {
        return nomeCliente;
    }
    public void setNomeCliente(String nomeCliente) {
        this.nomeCliente = nomeCliente;
    }
    public Long getNumeroCpf() {
        return numeroCpf;
    }
    public void setNumeroCpf(Long numeroCpf) {
        this.numeroCpf = numeroCpf;
    }

@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((email == null) ? 0 : email.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((numeroCpf == null) ? 0 : numeroCpf.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Cliente other = (Cliente) obj;
        if (email == null) {
            if (other.email != null)
                return false;
        } else if (!email.equals(other.email))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (numeroCpf == null) {
            if (other.numeroCpf != null)
                return false;
        } else if (!numeroCpf.equals(other.numeroCpf))
            return false;
        return true;
    }
}

嵌入ID

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class ClienteId implements Serializable{
    private static final long serialVersionUID = 1L;

    @Column(name="COD_EMP")
    private Long codigoEmpresa;

    @Column(name="COD_FIL")
    private Long codigoFilial;

    @Column(name="NUM_CLI")
    private Long numeroCliente;


    public Long getCodigoEmpresa() {
        return codigoEmpresa;
    }

    public void setCodigoEmpresa(Long codigoEmpresa) {
        this.codigoEmpresa = codigoEmpresa;
    }

    public Long getCodigoFilial() {
        return codigoFilial;
    }

    public void setCodigoFilial(Long codigoFilial) {
        this.codigoFilial = codigoFilial;
    }

    public Long getNumeroCliente() {
        return numeroCliente;
    }

    public void setNumeroCliente(Long numeroCliente) {
        this.numeroCliente = numeroCliente;
    }

@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((codigoEmpresa == null) ? 0 : codigoEmpresa.hashCode());
        result = prime * result + ((codigoFilial == null) ? 0 : codigoFilial.hashCode());
        result = prime * result + ((numeroCliente == null) ? 0 : numeroCliente.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ClienteId other = (ClienteId) obj;
        if (codigoEmpresa == null) {
            if (other.codigoEmpresa != null)
                return false;
        } else if (!codigoEmpresa.equals(other.codigoEmpresa))
            return false;
        if (codigoFilial == null) {
            if (other.codigoFilial != null)
                return false;
        } else if (!codigoFilial.equals(other.codigoFilial))
            return false;
        if (numeroCliente == null) {
            if (other.numeroCliente != null)
                return false;
        } else if (!numeroCliente.equals(other.numeroCliente))
            return false;
        return true;
    }
}

存储库

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import io.swagger.annotations.Api;


    @Api
    @Component
    public interface ClienteRepository extends JpaRepository<Cliente, ClienteId> {

        Cliente findByNumeroCpf(@Param("numeroCpf") Long numeroCpf);

        Cliente findByEmail(@Param("email") String email);  
    }

服务

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Path("/cliente")
@Transactional
public class ClienteService {

    @Inject
    private ClienteRepository repository;

    @Inject
    private CodMarcaRepository marcaRepository;

    @GET
    @Path("/cpf/{numeroCpf}")
    @Produces(MediaType.APPLICATION_JSON)
    @Transactional(readOnly=true)
    public Response findByCpf(@PathParam("numeroCpf") Long numeroCpf){
        Cliente cliente = repository.findByNumeroCpf(numeroCpf);

        if(cliente != null){
            return Response.ok().entity(cliente).build();
        } else {
            return Response.status(404).entity(new Cliente()).build();
        }
    }

    @GET
    @Path("/email/{email}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findByEmail(@PathParam("email") String email){
        Cliente cliente = repository.findByEmail(email);

        if(cliente != null){
            return Response.ok().entity(cliente).build();
        } else {
            return Response.status(404).entity(new Cliente()).build();
        }
    }
}

正如你所看到的,我的服务中有两种方法,一种是通过cpf查找,另一种是通过电子邮件查找,但都不起作用。我还尝试创建一个方法来通过组合主键查找,但它也不起作用

任何帮助都将被感激,因为我不知道还能做什么


共 (1) 个答案

  1. # 1 楼答案

    我不确定这是否是您遇到的问题的根源,但您没有覆盖equals()/hashCode()类的Embeddable。如果要将该类用作@EmbeddedId,则必须执行此操作