title: Hibernate get value of column from other table
body: <p>I have one Entity called Trade which can be mapped to a Table containing the attributes. Also this Entity, has a field, which stores the value from another table. My Trade table contains the primary key for the second table. I know how to get the whole second table as entity in my Trade entity, but i only want that 1 column. </p>
  
  <pre><code>@Entity
  @Immutable
  @Table(name = "Trade table")
  @SecondaryTables(value = {
      @SecondaryTable(name = "2nd table", pkJoinColumns = @PrimaryKeyJoinColumn(referencedColumnName = "id", foreignKey = @ForeignKey(name = "2ndtableID"))),
      @SecondaryTable(name = "3rd table", pkJoinColumns = @PrimaryKeyJoinColumn(referencedColumnName = "id", foreignKey = @ForeignKey(name = "3rdtableID"))) })
  public class TradeSearchResult {
      @Id
      @Column(name = "ID")
      private Long primaryKey;
      @Column(name = "col2", table = "2nd table")
      private String valuefrom2ndtable;
      @Column(name = "col3", table = "3rd table")
      private String valuefrom3ndtable;
  }
  </code></pre>
  
  <p>as you can see i tried with <code>@SecondaryTables</code>, but the join were executed on the primaryKey from Trade entity. How can I use annotations to select the foreign key from trade table, join it to 2nd/3rd table and directly get only the value from col2/col3? Any suggestion is appreciated </p>
  
  <p>Table Trade:</p>
  
  <pre><code>ID    col1   fk1    fk2
  ------------------------
  1     abc    12     43      
  2     def    23     32
  3     ghi    34     21
  </code></pre>
  
  <p>Table 2:</p>
  
  <pre><code>ID    col2
  ----------
  12    a
  23    b
  34    c
  </code></pre>
  
  <p>Table 3:</p>
  
  <pre><code>ID    col3
  -----------
  43    d
  32    e
  21    f
  </code></pre>
  
  <p>now my Trade class should have the Attributes:</p>
  
  <pre><code>Long id;
  String col1;
  String col2;
  String col3;
  </code></pre>
  
  <p>The hibernate query: <code>from Trade</code> should give me in this example 3 Trades with the attributes:</p>
  
  <pre><code>Trade1: 1, "abc","a","d"
  Trade2: 2, "def","b","e"
  Trade3: 3, "ghi","c","f"
  </code></pre>
  
  <p>and i don't want to create an entity only to reach to the attributes <code>col2</code> and <code>col3</code></p>
  
  <p>EDIT:</p>
  
  <p>A select statement would look like this:</p>
  
  <pre><code>select trade.ID,TICKET,table2.col2,table3.col2 from trade join table 3 on table3.ID=trade.FKtable3 join table2 on table2.ID=trade.FKtable2
  </code></pre>
  
  <p>if i execute this statement in sql server, it would give me the result i want to have. i want to map my class, so that hibernate generates this statement.</p>