JpaRepository找不到具有EmbeddedID的对象 [英] JpaRepository can't find object with EmbeddedID

查看:125
本文介绍了JpaRepository找不到具有EmbeddedID的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JpaRepository通过ID来从数据库中获取对象,并且遇到了一些麻烦.这是具有EmbeddedId的实体.

I'm trying to use a JpaRepository to get an object from the database by its ID and an having some trouble. It's an entity with an EmbeddedId.

我正在尝试通过两种不同的方式获取对象:

I'm trying to get the object two different ways:

  1. 使用命名查询(findById)
  2. 使用SowServiceImpl中的方法

当我尝试使用命名方法获取它时遇到的异常是:

The exception I get when trying to get it using the named method is:

@Override
public SowDocument getById(int i) {
    SowDocument wow = sowRepository.findById(i);
    if (wow == null) {
        System.out.println("NULLLLLLLLLLLLLLLL");
        return null;
    } else {
        return wow;
    }
}

HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [1] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
...
...
at com.sun.proxy.$Proxy791.findById(Unknown Source)
at com.**.pricing.web.services.impl.SowServiceImpl.getById(SowServiceImpl.java:61)
at com.**.pricing.web.controllers.UserController.getSowById(UserController.java:99)

当我尝试使用EmbeddedId获取它时,我得到了NullPointerException:

And I get a NullPointerException when I try getting it using the EmbeddedId:

@Override
public SowDocument getById(int i) {
    SowDocumentPK peek = new SowDocumentPK();
    peek.setId(i);

    SowDocument wow = sowRepository.findOne(peek);
    if (wow == null) {
        System.out.println("NULLLLLLLLLLLLLLLL");
        return null;
    } else {
        return wow;
    }
}

这里是对象

@Entity
@Table(name = "SowDocument", uniqueConstraints = {
    @UniqueConstraint(columnNames = {"id"})})
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "SowDocument.findAll", query = "SELECT s FROM SowDocument s"),
    @NamedQuery(name = "SowDocument.findById", query = "SELECT s FROM SowDocument s WHERE s.sowDocumentPK.id = :id"),
    @NamedQuery(name = "SowDocument.findByClientName", query = "SELECT s FROM SowDocument s WHERE s.clientName = :clientName"),
    @NamedQuery(name = "SowDocument.findByCreationDate", query = "SELECT s FROM SowDocument s WHERE s.creationDate = :creationDate"),
    @NamedQuery(name = "SowDocument.findByDocumentCreator", query = "SELECT s FROM SowDocument s WHERE s.sowDocumentPK.documentCreator = :documentCreator"),
    @NamedQuery(name = "SowDocument.findBySowType", query = "SELECT s FROM SowDocument s WHERE s.sowType = :sowType")})
public class SowDocument implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected SowDocumentPK sowDocumentPK;
    @Size(max = 50)
    @Column(name = "clientName", length = 50)
    private String clientName;
    @Size(max = 45)
    @Column(name = "creationDate", length = 45)
    private String creationDate;
    @Lob
    @Column(name = "data")
    private byte[] data;
    @Size(max = 45)
    @Column(name = "sowType", length = 45)
    private String sowType;
    @JoinColumn(name = "documentCreator", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private User user;

    public SowDocument() {
    }

    public SowDocument(SowDocumentPK sowDocumentPK) {
        this.sowDocumentPK = sowDocumentPK;
    }

    public SowDocument(int id, int documentCreator) {
        this.sowDocumentPK = new SowDocumentPK(id, documentCreator);
    }

    public SowDocumentPK getSowDocumentPK() {
        return sowDocumentPK;
    }

    public void setSowDocumentPK(SowDocumentPK sowDocumentPK) {
        this.sowDocumentPK = sowDocumentPK;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public String getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(String creationDate) {
        this.creationDate = creationDate;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public String getSowType() {
        return sowType;
    }

    public void setSowType(String sowType) {
        this.sowType = sowType;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (sowDocumentPK != null ? sowDocumentPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof SowDocument)) {
            return false;
        }
        SowDocument other = (SowDocument) object;
        if ((this.sowDocumentPK == null && other.sowDocumentPK != null) || (this.sowDocumentPK != null && !this.sowDocumentPK.equals(other.sowDocumentPK))) {
            return false;
        }
        return true;
    }


}

这是它的嵌入式ID:

@Embeddable
public class SowDocumentPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private int id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "documentCreator", nullable = false)
    private int documentCreator;

    public SowDocumentPK() {
    }

    public SowDocumentPK(int id, int documentCreator) {
        this.id = id;
        this.documentCreator = documentCreator;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getDocumentCreator() {
        return documentCreator;
    }

    public void setDocumentCreator(int documentCreator) {
        this.documentCreator = documentCreator;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (int) id;
        hash += (int) documentCreator;
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof SowDocumentPK)) {
            return false;
        }
        SowDocumentPK other = (SowDocumentPK) object;
        if (this.id != other.id) {
            return false;
        }
        return this.documentCreator == other.documentCreator;
    }


}

这是SowRepository的代码:

Here's the code for the SowRepository:

public interface SowRepository extends JpaRepository<SowDocument, Serializable> {

    SowDocument findById(int i);

}

以下是SowService的代码:

Here's the code for the SowService:

@Service
public class SowServiceImpl implements SowService {

    @Autowired
    private SowRepository sowRepository;

    @Override
    public void save(SowDocument sow) {
        sowRepository.save(sow);
    }

    @Override
    public Collection<SowDocument> getAll() {
        return sowRepository.findAll();
    }

    @Override
    public SowDocument getById(int i) {
        SowDocumentPK peek = new SowDocumentPK();
        peek.setId(i);
        return sowRepository.findOne(i);
    }


}

我的猜测是我以某种方式在SowDocument/SowDocumentPK之间存在错误的映射,或者误解了如何使用JpaRepository.

My guess is that I somehow have the mapping(s) wrong between SowDocument/SowDocumentPK or am mis-understanding how to use the JpaRepository.

推荐答案

我猜您正在使用SpringData JPA.如果正确,请查看如何定义JpaRepostory接口的超级类型CrudRepository:

I guess you are using SpringData JPA. If this is correct, look how the CrudRepository which is the super type of JpaRepostory Interface is defined:

public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
     ...
     T findOne(ID id);
     ...
}

您正在将该接口扩展为(顺便说一句,您也应该发布了实现):

And you are extending this interface as (btw you should have posted the implementation too):

public interface SowRepository extends JpaRepository<SowDocument, Serializable> {
    SowDocument findById(int i); 
}

findById(int i)方法是在JpaRepository类型层次结构中的接口的 none 中定义的,这意味着它是您自己的扩展名.

The findById(int i) method is defined in none of the interfaces in the JpaRepository type hierarchy which means it is your own extension.

另一方面,您没有ID为int类型的实体.您实体的ID类型定义为ShowDocumentPK类型,该类型由两个字段组成.

On the other hand you don't have an entity with a ID of type int. The type of the ID of your entity is defined to be of type ShowDocumentPK that consists of two fields.

因此您的存储库定义应如下所示:

So your repository definition should look like the following:

public interface SowRepository extends JpaRepository<SowDocument, ShowDocumentPK> {
     SowDocument findById(ShowDocumentPK id); 
}

并自己实现方法findById()(或使用JpaRepository的官方实现类,即SimpleJpaRepository).

And implement the method findById() by yourself (or use the official implementation class of the JpaRepository, i.e., SimpleJpaRepository).

然后您应该创建ShowDocumentPK的实例并将其传递给findById()方法,例如:

And then you should create an instance of ShowDocumentPK and pass it to the findById() method, for example:

SowDocumentPK peek = new SowDocumentPK();
peek.setId(1);
peek.setDocumentCreator(100);

SowDocument wow = sowRepository.findById(peek);

结论:您的ID并非int类型,并且findById(int)不是您的情况下实现方式.

In conclusion: your ID is not of type int and findById(int) is not the way how to implement in your case.

希望这为您提供了一个正确实施方法的想法.

Hope this gives you an idea how to implement it correctly.

这篇关于JpaRepository找不到具有EmbeddedID的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆