title: How to replace a @JoinColumn with a hardcoded value?
body: <p>For more context, please see <a href="https://stackoverflow.com/questions/27824565/jpa-fk-on-subset-of-pk-table-to-eliminate-composite-key-need">my other question</a>.</p>
  
  <p>I want to join my <code>Employee</code> entity class to <code>Code</code> entity class but the Code PK is composite (DOMAIN, CODE) because it lists many different code domains, yet the codes that go in the Employee class/table are all from within a single domain, eliminating the need to have a domain field in the Employee class/table (because it would always be the same).</p>
  
  <p>Can I join <em>Employee</em> to <em>Code</em> by using the CODE field in the <em>Employee</em> table and a hardcoded value (e.g. <em>EMP_TYPE</em>) instead of a redundant column?</p>
  
  <p>If my <em>Employee</em> class/table did indeed have that redundant column, I would join it like this:</p>
  
  <pre><code>@JoinColumns({
          @JoinColumn(name = "EMP_TYPE_CODE", referencedColumnName = "CODE"),
          @JoinColumn(name = "DOMAIN", referencedColumnName = "DOMAIN)})
      @ManyToOne(optional = false)
      private Code sharedStatute;
  </code></pre>
  
  <p>but I REALLY don't want to have that extra column in the DB and in the class because it will always be the same.</p>
  
  <p>What I am trying to accomplish would be equivalent to the following in SQL:</p>
  
  <pre><code>SELECT e.emp_id, e.first_name, e.last_name, c.description as emp_type
  FROM Employee e JOIN Code c
  ON e.emp_type_code = c.code
  WHERE c.domain = 'EMP_TYPE'
  </code></pre>
  
  <p>as opposed to adding a field <code>domain</code> in the Employee table and populating EVERY SINGLE RECORD with the same value ('EMP_TYPE') and then doing:</p>
  
  <pre><code>SELECT e.emp_id, e.first_name, e.last_name, c.description as emp_type
  FROM Employee e JOIN Code c
  ON e.emp_type_code = c.code
  AND e.domain = c.domain
  </code></pre>
  
  <p>The former is more efficient because it saves me from having to have a redundant field.  So what I am trying to do is the same thing but in <code>JPA</code>.</p>
  
  <p>Some of you may say something to the effect of "why not have a separate lookup table for each code" but I think that's a terrible idea and causes cluttering of DB tables and corresponding application entities.  It is much better to have a single code lookup table partitioned by code type (or domain).</p>