title: design: same logical computations needed both in database and in Java
body: <p>I am designing a web application that retrieves records from a database according to user search criteria. After retrieving results, another stage exists in the flow in which the results are "decorated" with additional data.</p>
  
  <p>For example, we may have a Persons database table with columns such as: name, age, height, weight. The program can filter that table by a certain logical computation formula, e.g. keep all Person records where the person's weight is less than her "ideal weight", defined as:</p>
  
  <pre><code>IDEAL_WEIGHT = (height/100) * age
  </code></pre>
  
  <p>So, we will show all records where IDEAL_WEIGHT &lt; weight, this can be accomplished with SQL, stored procedures, etc - bottom line is that <strong>we filter with SQL</strong>. Concerning the "results decoration" stage, it can be, for example, calculating the WEIGHT_PER_INCH for each result, defined as</p>
  
  <pre><code>WEIGHT_PER_INCH = height / weight
  </code></pre>
  
  <p>These "decoration" computations may be a lot more complex than the above, and are actually broader than mathematical formulas - they may involve data residing on remote hosts, or rely on other computations units. So - it seems SQL and probably PL/SQL are not suited for the job, rather Java is - bottom line is that <strong>we "decorate" with Java</strong>.</p>
  
  <p>Now, what we mentioned above is filtering by IDEAL_WEIGHT and decorating with WEIGHT_PER_INCH, but <strong>what happens if we want to be able to have it the other way around?</strong> (i.e. filter by WEIGHT_PER_INCH and decorate with IDEAL_WEIGHT).</p>
  
  <p>You may say that all these computation units should be of one type - either stored SQL procedures, or Java classes. But if they're SQL procedures they are not expressive enough, and if they are Java classes, they cannot be used to search a database quickly.</p>
  
  <p>My question is:</p>
  
  <p><strong>Is there a clean, widely-used, fast way to invoke Java from SQL</strong>, e.g.</p>
  
  <pre><code>Select * from Persons p where IDEAL_WEIGHT(p) &lt; p.weight
  </code></pre>
  
  <p>where IDEAL_WEIGHT() is a Java method, not an SQL procedure.</p>
  
  <p>or: <strong>Is there some other way around all this?</strong></p>
  
  <p>Thanks a bunch for your thoughts.</p>