title: Dilemma while using hibernate
body: <p>I'm using two tables for my current project(it will grow certainly).</p>
  
  <p>First table has like loads of columns i.e.</p>
  
  <pre><code>id|name|surname|age|token         |currentpackage|difference
  1 |me  | you   | 22|xc2e2144124sd |100           |0
  </code></pre>
  
  <p>Second table :</p>
  
  <pre><code>id|mirror|token|        connsize|price
  81|server|xc2e2144124sd|100mbps |1000
  </code></pre>
  
  <p>These tables are full of data. There are some rows in the table1 that have same token value as the one in the table2.</p>
  
  <p>I've googled for solution for quite alot, this is the best I found :</p>
  
  <p><a href="http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/" rel="nofollow noreferrer">http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/</a>
  and
  <a href="http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-one-1.html" rel="nofollow noreferrer">http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-one-1.html</a></p>
  
  <p>But I don't think these apply to my case(or do they). In these examples the relation is key which is incremental, my token is just the way it is in the database.</p>
  
  <p>My final goal is to be able to calculate the difference between currentpackage and price</p>
  
  <p>What would you recommend for this? I'm using java/hibernate/spring</p>
  
  <p>Is there any other way to tell hibernate to link by something else other that primary key?
  some custom key like token in table 1? token is some kind relation between table one and table two. Its many(table1) to one(table2).</p>
  
  <p><strong>LATEST UPDATE</strong></p>
  
  <p>I'm trying to do it by using query to substract these two columns :</p>
  
  <pre><code>UPDATE packages tl 
  SET difference = (select (t2.price-t1.currentpackage) 
  from packages t1, mirrors t2 
  where t2.token= t1.token)
  </code></pre>
  
  <p>I got this error :</p>
  
  <pre><code>ERROR:  more than one row returned by a subquery used as an expression
  </code></pre>
  
  <p>I must have tried many options but none worked</p>
  
  <p>Tried this as well same result :</p>
  
  <pre><code>UPDATE packages X 
  SET difference = (select Y.price-X.currentpackage
  FROM packages X
  LEFT OUTER JOIN mirrors Y
  ON Y.token = X.token)
  </code></pre>
  
  <p>This seems to be what I'm looking for but I'm not able to incorporate it in my query :</p>
  
  <p><a href="https://stackoverflow.com/questions/5254859/subtract-values-from-two-different-tables/5254939#5254939">Subtract Values from Two Different Tables</a></p>