I want to assign SQL query result to Java object which is in non-entity class. My query is counting the number of records in Table A mapped to another Table B.
@Query(value="select count(a.id) from table1 a join table2 b on a.id=b.id group by a.id", nativeQuery=true)
Non-Entity class
public class Sample {
//assign query result to count variable
private long count;
// getters and setters
}
A and B are Entity class, I'm selecting specified columns of Entity A and B and including that columns in Sample.class and sending data as JSON on REST call.
Now my question is to assign count result to count variable.
Thanks in advance
countis a private variable you will have to create a setter for it and use it to assign the value. Why don't you follow naming conventions, the class name should start with a capital letter. Also the comment is misleading sincecountis not an object.Sample sample = new Sample(); sample.setCount(countResult);wherecountResulthas a value from the query. Of course if you have a reference of theSampleclass you don't have to initialise it.