Best Way To Get Aggregate Function Result Inside The Entity Object
Many time I have to get the SQL aggregate query result inside the Entity Object itself. As of now I could able to achive the same by the following code CriteriaBuilder cb = em.
Solution 1:
If you cannot use @Formula then I'd suggest create a database view basic on your select and mapping an additional entity to that. You can then map this to your existing entity using either as a @OneToOne or by using @SecondaryTable.
This has the added advantage of being JPA compliant (i.e. not using Hibernate's propreitary @Formula) and would look something like:
@Entity@Table(name = "test")
public class Test {
@Id@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "type")
private Integer type = 0;
@OneToOne//or via secondary table
private TestSummaryInfo summaryInfo;
publiclonggetQuantity(){
returnsummaryInfo.getQuantity();
}
}
Summary mapped to a view:
@Entity@Table(name = "vw_test_summary")
public class TestSummaryInfo {
@Id@Column(name = "id")
private Integer id;
@Column(name = "quantity")
private Long quantity;
}
Post a Comment for "Best Way To Get Aggregate Function Result Inside The Entity Object"