java - JavaSpring中的SQL查询返回Null
问题描述
我正在尝试通过 Java Spring 进行简单的 SQL 调用。数据库有数据,但在调用时返回空值列表。
有趣的是,当我选择特定行时,它会返回正确的值。(见下文)
BasicAccountAuditRepo.java
@Repository
public interface BasicAccountAuditRepository extends CrudRepository<BasicAccountAudit, BasicAccountAuditPK> {
List<BasicAccountAudit> findAll();
//THIS RETURNS NULL
@Query("SELECT b FROM BasicAccountAudit as b WHERE id.accountRef = :accountRef ")
List<BasicAccountAudit> findByAccountRef(@Param("accountRef") String accountRef);
//This returns the correct values for dcConnName
@Query("SELECT id.dcConnName FROM BasicAccountAudit WHERE id.accountRef = :accountRef ")
List<BasicAccountAudit> findByAccountRef(@Param("accountRef") String accountRef);
}
我正在为模型类使用嵌入式 ID。BasicAccountAudit.java
@XmlRootElement
@Entity
@Table(name = "tb_Account_History", schema="dbo")
public class BasicAccountAudit implements Serializable{
@EmbeddedId
private BasicAccountAuditPK id;
@Temporal(TemporalType.TIMESTAMP)
@Column(insertable = true, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private Date enteredDate;
@Temporal(TemporalType.TIMESTAMP)
@Column(insertable = true, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private Date lastUpdatedDate;
}
这是主键类:BasicAccountAuditPK.java
package org.fusion.restful.basicaccount.model;
import java.io.Serializable;
public class BasicAccountAuditPK implements Serializable {
private String accountRef;
private String client;
private String dcEligible;
private String shortCode;
private String loadCps;
private String stpFlag;
private String accountType;
private String clearingFirm;
private String exchange;
private String dcConnName;
private String status;
private String enteredBy;
//getters and setters...
}
解决方案
您需要与别名用法保持一致。在您的第一个查询中,您忘记了where
子句中的别名:
"SELECT b FROM BasicAccountAudit as b WHERE id.accountRef = :accountRef "
应该
SELECT b FROM BasicAccountAudit as b WHERE b.id.accountRef = :accountRef "
在您的第二个查询中,您没有声明别名,因此默认情况下它会在您的对象下查找 id。
推荐阅读
- c# - 删除不同类型的对象
- c++ - 在调试版本上运行在发布版本上失败
- sql-server - Sql Server LDF 文件占用的空间太大
- google-chrome-extension - 匹配 IP 地址 (content_scripts)
- teamcity - 在 Windows 10 中使用 TeamCity 2018 进行持续部署
- python - for循环和多个CSV文件的问题
- php - 使用 php web hook 修改 HTML 元素
- python - 如何在 ANTLR 中定义起始规则?
- javascript - SVG 到图像导出性能问题(使用 canvg / XMLSerializer / getComputedStyle)
- php - 发布列