请注意账户id并不是唯一的(为简单起见我删除表中的其他参数,使其短)。 一旦我筛选记录,我必须更新LookupRequest。 回调foreignkey条目。 我无法外键为空时检索记录。
import javax.persistence.*;
@Entity
public class LookupRequest {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@Column
private String account;
@Column
private String caseId;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "id", unique=false, nullable=true, updatable=true)
private Callback callback;
public LookupRequest(String account, String caseId) {
this.account = account;
this.caseId = caseId;
callback = null;
}
public Long getId() {
return id;
}
public String getAccount() {
return account;
}
public String getCaseId() {
return caseId;
}
public Callback getCallback() {
return callback;
}
public void setCallback(Callback callback) {
this.callback = callback;
}
}
@Repository
public interface LookupRequestRepository extends JpaRepository<LookupRequest, Long> {
@Query("SELECT lr from LookupRequest lr where lr.account = :account and lr.callback = :callback")
LookupRequest findCaseId(@Param("account") String account, @Param("callback") String callback);
}
@Getterbr/>@Setter
@Entity
public class Callback {
@Id @GeneratedValue(strategy= GenerationType.AUTO) public Long id; @Column(length = 20000) private String rawResponse; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public void setRawResponse(String rawResponse) { this.rawResponse = rawResponse; }
}
然后我打电话如下:
LookupRequest lookupRequestFromDB = lookupRequestRepository.findCaseId("ABC", null);什么错了吗?