Entity Class
import com.vladmihalcea.hibernate.type.array.IntArrayType;
import com.vladmihalcea.hibernate.type.array.StringArrayType;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.*;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@TypeDefs({
@TypeDef(
name = "string-array",
typeClass = StringArrayType.class
),
@TypeDef(
name = "int-array",
typeClass = IntArrayType.class
),
@TypeDef(name = "json", typeClass = JsonStringType.class),
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
@Entity
@Table(name="combination_holding_rule")
public class CombHoldingRule {
@Id
@Column(name="combination_hr_id")//Checked
private Integer combHoldingRuleId;
@Column(name="combination_hr_name")//Checked
private String combHoldingRuleName;
@Column(name="jurisdiction_id")//Checked
private Integer jurisdictionId;
@Column(name="function_group_id")//Checked
private Integer functionGroupId;
@Column(name="overall_netting_type")//Checked
private String overallNettingType;
@Column(name="package_id")//Checked
private Integer packageId;
@Type(type = "int-array")
@Column(
name = "holdingrule_list",
columnDefinition = "integer[]"
)
private int[] holdingRuleList;
}
In Repository
@Query(value="from CombHoldingRule where packageId=:packageId")
List<CombHoldingRule> repoCHRFromPackageId(@Param("packageId") int packageId);
I took the result from JPAQuery into the Entity , then did the below in the Service Layer
public List<CHRfromPackageIdDTO> getCHRFromPackageIdService(int packageId) {
List<CombHoldingRule> combHoldingRuleList = combinationHRrepo.
repoCHRFromPackageId(packageId);
List<CHRfromPackageIdDTO> combDTO = new ArrayList<>();
for ( CombHoldingRule combHoldingRule : combHoldingRuleList) {
CHRfromPackageIdDTO temp = new CHRfromPackageIdDTO(combHoldingRule.getCombHoldingRuleId(),
combHoldingRule.getCombHoldingRuleName(),
combHoldingRule.getHoldingRuleList());
combDTO.add(temp);
}
return combDTO;
}
Also pls check HERE
Note: This is kind of work around I believe, really not sure , how to directly take value from a native query into a custom Pojo instead of an entity class. I really appreciate if anyone post the answer for that. I would accept that as the Answer.