2016-07-27 63 views
0

我想使用JpaRepository从数据库中获取对象的ID和一些麻烦。它是一个带有EmbeddedId的实体。JpaRepository无法找到对象与嵌入式ID

我试图获取对象两种不同的方式:

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

异常努力,当我得到得到它使用的命名方法是:

@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) 

而且我得到一个NullPointerException时我尝试使用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代码:

public interface SowRepository extends JpaRepository<SowDocument, Serializable> { 

    SowDocument findById(int i); 

} 

这里是对于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之间的错。

+0

'ShowDocumentPK'有两个字段,但您创建'SowDocumentPK peek = new SowDocumentPK(); peek.setId(i);'并作为主键传递。它是故意的吗? – ujulu

+0

如果我只想通过自己的ID获取对象,而不是完整的组合,是否需要指定BOTH字段? – Zeratas

+0

我试图解释如何在下面的答案中使用它。看看它是否对你有所帮助;否则,留下我的评论。 – ujulu

回答

1

我想你正在使用SpringData JPA。如果这是正确的,看看怎么CrudRepository这是超类型JpaRepostory接口的定义:

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

而且要扩展这个接口(顺便说一句,你应该已经发布实施过):

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

findById(int i)方法在JpaRepository类型层次结构,这意味着它是自己的分机接口没有定义。

在另一方面,你不必与int类型的ID的实体。实体ID的类型被定义为由两个字段组成的ShowDocumentPK类型。

所以你的资料库的定义应该如下所示:

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

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

然后你应该创建的ShowDocumentPK一个实例,并把它传递给findById()方法,例如:

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

SowDocument wow = sowRepository.findById(peek); 

结论:你的ID是int类型和findById(int)不是不怎样的方式来实现在你的情况。

希望这给你一个想法如何正确实施它。

+0

我其实自己做了findById。我在看到CRUDRepository之前就已经说过了,并且在我发现之前也发现了相同的结论。 现在可以使用NamedQuery进行工作了。 – Zeratas