Spring-Data-JPA - 查询返回null - EmbeddedId

时间:2017-09-18 18:32:21

标签: java spring hibernate jpa spring-data-jpa

我有一个带有嵌入式ID的课程。当我尝试使用Jpa Repository进行搜索时,它只返回null对象。看起来问题出现在嵌入式身份验证中,我没有使用过这个类的测试,它运行良好。

JPA在控制台中输出的查询在我对数据库进行测试时工作正常。

并没有错误输出。

编辑:数据库中有数据

EDIT2:添加了equals和hashcode。

EDIT3: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;
    }
}

EmbeddedId

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 个答案:

答案 0 :(得分:0)

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

,则必须执行此操作