简体   繁体   中英

Getting empty result from the @Query in Hibernate

I am using Hibernate to execute my query, in admin panel i am getting correct result but while using in Hibernate it is not giving any result.

Dao layer -

@Query("select new com.eventila.pms.entity.ReferenceLead(projectId,count(lm)) from LeadMaster lm where lm.vendorId= ?1 and lm.source = 'share' group by lm.projectId")
List<ReferenceLead> getReferenceByUser(String userId);

Pojo -

@lombok.Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ReferenceLead {

    String projectId;
    Long referenceLead;
    Long count;

    protected ReferenceLead(){}


    public ReferenceLead(String projectId,Long count) {
        this.projectId=projectId;
        this.count=count;
    }

}

After executing this i am getting a empty list. Please help me out.

In your select query return the fields without calling new constructor:

@Query("select projectId, count(lm) from LeadMaster lm where lm.vendorId = ?1 and lm.source = 'share' group by lm.projectId")
List<ReferenceLead> getReferenceByUser(String userId);

Hibernate will instantiate the object using these fields. Also, add @Entity annotation to your ReferenceLead class.

'source' is the keyword in SQL.

It is a keyword used in MERGE. ie WHEN NOT MATCHED BY SOURCE.

The word MATCHED also exhibits the same behaviour in that it gets highlighted grey in the editor.

Neither of these are reserved keywords though so if used as an identifier they do not need to be delimited (unless you find the syntax highlighting distracting).

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