Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use @Formula (in Hibernate) to refer to the same (current) object

I have this class

@Entity
@Table(name = "DB.APPL_SESSION")
@AttributeOverrides({@AttributeOverride(name = "id", column = @Column(name = "APPL_SESSION_ID"))})
@SequenceGenerator(name = "TableSequence", sequenceName = "DB.APPL_SESSION_SQ")

public class AiSession{

@Formula("(select sum(nvl(budg.AMT_OV,budg.AMT)) from DB.BUDGET budg where budg.APPL_SESSION_ID = APPL_SESSION_ID)")
private LocalDate realSessionStartDate;

In the above formula I was working with DB.BUDGET object and with the current object id ( APPL_SESSION_ID). But now I wanted only to work with the next member variable (StartDate) which is in AidApplicantYearSession class

@Basic
@Temporal(TemporalType.DATE)
@Column(name = "START_OV_DT")
private Date overrideStartDate;

How should I write a formula that looks like the above on but uses only the object's member variable ? I came up with

 @Formula("(select START_OV_DT from DB.APPL_SESSION)")

Is that the correct way? The logic for the two formulas is totaly different.

like image 397
WowBow Avatar asked Jan 11 '12 20:01

WowBow


1 Answers

@Formula is simply inserted into the SQL select clause, so that you can use column names in it:

@Formula("START_OV_DT")

See also:

  • 5.1.4.1.5. Formula
like image 172
axtavt Avatar answered Nov 03 '22 22:11

axtavt