Skip to content
Advertisement

NamedQuery returning entities with null fields

defined entity with namedquery as SELECT mdl FROM tbl_slots mdl where mdl.test_date between :dt and :dt order by mdl.test_time asc

if used * instead of mdl in query, JPA gives error unexpected token: *

if mentioned the column names in select statement it returns entities with respective fields populated with expected values

[{ “srNo”: 1, “testDate”: “2021-Dec-30”, “testTime”: “09:00-10:00”, },{ “srNo”: 2, “testDate”: “2021-Dec-30”, “testTime”: “11:00-12:00”, }]

how to get same result without mentioning column names in select statement as if used *?

what’s valid JPQL or HQL for select * from table query?

Entity class

@Table(name = "tbl_slots")
@NamedQueries({
    @NamedQuery(name="slots",query = "SELECT mdl FROM tbl_slots mdl where  mdl.test_date between :dt and :dt order by mdl.test_time asc")
})
public class TblSlots implements Serializable {
    private Long srNo;
    private Date testDt;
    private String testTime;
        
    public TblSlots() {}
    public TblSlots(Long srNo, Date testDt, String testTime) {
        this.srNo = srNo;
        this.testDt = testDt;
        this.testTime = testTime;
    }
    
    @Id
    @Column(name = "sr_no", unique = true, nullable = false, precision = 16, scale = 0)
    public Long getSrNo() {
        return this.srNo;
    }

    public void setSrNo(Long srNo) {
        this.srNo = srNo;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "test_date", nullable = false, length = 13)
    public Date getTestDt() {
        return this.testDt;
    }

    public void setTestDt(Date testDt) {
        this.testDt = testDt;
    }

    @Column(name = "test_time", nullable = false, length = 20)
    public String getTestTime() {
        return this.testTime;
    }

    public void setTestTime(String testTime) {
        this.testTime = testTime;
    }

Advertisement

Answer

JPQL understand java entities and not columns.

how to get same result without mentioning column names in select statement as if used *?

what’s valid JPQL or HQL for select * from table query?

So a valid JQPL SELECT query can even not have the SELECT clause.

The following would be a valid JPQL query to return the complete entities from that table

FROM tbl_slots mdl where  mdl.test_date between :dt and :dt order by mdl.test_time asc

So your annotation could be written as

@NamedQueries({
    @NamedQuery(name="slots",query = "FROM tbl_slots mdl where  mdl.test_date between :dt and :dt order by mdl.test_time asc")
})
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement