简体   繁体   中英

SQL Query in JavaSpring Returning Null

I'm trying to do a simple SQL call through Java Spring. The database has data but on the call is returning a list of null values.

What's interesting is that when I select a particular row, it returns the correct value. (See below)

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

}

I'm using an embedded ID for the model class. 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;


}

Here is the Primary Key Class: 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...
}

You need to be consistent with the alias usage. In your first query you have forgotten the alias in your where clause:

"SELECT b FROM BasicAccountAudit as b WHERE id.accountRef  = :accountRef "

should be

SELECT b FROM BasicAccountAudit as b WHERE b.id.accountRef  = :accountRef "

In your second query you don't declare alias therefore by default it is looking up the id under your object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM