![](/img/trans.png)
[英]Java returning null pointer exception for SQL query that gets passed to JSP
[英]SQL Query in JavaSpring Returning 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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.