私はJpaRepositoryを使用してIDからデータベースにオブジェクトを取得しようとしていますが、何か問題があります。これはEmbeddedIdを持つエンティティです。JpaRepositoryがEmbeddedIDでオブジェクトを見つけることができません
私はオブジェクト二つの異なる方法を取得しようとしている:SowServiceImplのメソッドを使用して名前付きクエリ(findById)
しようとしたとき、私が得る例外名前付きメソッドを使用して取得します。
@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の使用方法を誤って理解していますとの間違ったマッピングを持っているということです。
'ShowDocumentPK'には2つのフィールドがありますが、' SowDocumentPK peek = new SowDocumentPK(); peek.setId(i); 'を呼び出し、主キーとして渡します。それは慎重ですか? – ujulu
完全なコンポジットではなく、独自のIDでオブジェクトを取得したい場合は、両方のフィールドを指定する必要がありますか? – Zeratas
私は以下の答えでそれを使用する方法を説明しようとしました。それがあなたを助けるかどうかを見てください。それ以外の場合は、私にコメントを残す。 – ujulu