title: Hibernate use property fields of an object in formula annotation
body: <p>I am fairly new to hibernate, and I am running into an issue that I am trying to overcome with very basic code just to get it to the point of working.  Here is what I have:</p>
  
  <pre><code>@Entity
  @Table(name="budgetfunds")
  public class BudgetItem implements Serializable{
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name="budgetFundsID")
      private int budgetFundID;
  
      @Column(name="budgetYear")
      private int budgetYear;
  
      @ManyToOne
      @JoinColumn(name = "semesterID")
      private Semester semester;
  
      @ManyToOne
      @JoinColumn(name = "fundCategoryID")
      private FundCategory fundCategory;
  
      @OneToMany
      @JoinColumns({
          @JoinColumn(name = "fundCategoryID", referencedColumnName = "fundCategoryID"),
          @JoinColumn(name = "semesterID", referencedColumnName = "semesterID"),
          @JoinColumn(name = "year", referencedColumnName = "budgetYear")
      })
      private Set&lt;Fund&gt; fundItems;
  
      @Column(name="budgetID")
      private int budgetID;
  
      @Column(name="budgetFundAmount")
      private BigDecimal fundAmount;
  
      @Formula(value = "select sum(f.fundAmount) from Fund f where f.fundCategoryID = fundCategoryID and f.semesterID = semesterID and f.year = budgetYear")
      private BigDecimal requestedAmount;
  
      ...
  }
  </code></pre>
  
  <p>Here is my problem.  I have a set of fundItems that I need to sum and set as this object's property so that it can be referenced in jsp. The last property is my attempt to do that... the only problem is, it is not valid sql because it doesn't know where the semesterID, budgetYear, and fundCategoryID values are coming from. Is there a way to fix this query to work <strong>per object</strong>?</p>
  
  <p>Also, if there is an easier way to do this without completely refactoring code/database let me know.</p>
  
  <p>Here is a diagram of the database and relations:
  <a href="https://i.stack.imgur.com/3IuI3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3IuI3.png" alt="enter image description here"></a></p>