title: How to add row in table only if that row is not present using hibernate?
body: <p>I have a table in database which has following structure(data) : </p>
  
  <pre><code>+--------------------------+
        Organization
  +--------------------------+
      org_id | name
  ---------------------------
     1       | IBM
  ---------------------------
     2       | Google
  ---------------------------
     3       | Yahoo
  +--------------------------+
  </code></pre>
  
  <p>In the above table, org_id is the primary key. I want to insert new row in this table only new organization name is found.</p>
  
  <p>Other tables which are supposed to use this table are as follows : </p>
  
  <pre><code>+--------------------------+
        Project
  +--------------------------+
   project_id| description
  ---------------------------
     1       | foo
  ---------------------------
     2       | bar...
  ---------------------------
     3       | xyz
  +--------------------------+
  </code></pre>
  
  <p>and the table which connects project and Organization table is as follows : </p>
  
  <pre><code>+--------------------------+
        Project_Client
  +--------------------------+
  pc_id | project_id | org_id
  ---------------------------
     1  | 1          | 1
  ---------------------------
     2  | 1          | 3
  ---------------------------
     3  | 2          | 2
  +--------------------------+
  </code></pre>
  
  <p>I want to implement something like this using hibernate :</p>
  
  <p>Each project has its description and a list of client names. Client name is nothing but organization name. So to avoid duplicate data I have created table named <code>Organization</code>. </p>
  
  <p>While inserting project information in database, if any client name already exist in <code>Organization</code> table then <code>org_id</code> of that client name should be used while inserting row in <code>Project_Client</code> table. If client name is not present in <code>Organization</code> table then new row should get added in <code>Organization</code> table and then corresponding <code>org_id</code> should be used in <code>Project_Client</code> table.</p>
  
  <p>Can we implement something like this using hibernate?</p>
  
  <blockquote>
    <p>EDIT #1</p>
  </blockquote>
  
  <p>Earlier I had classes as follows :</p>
  
  <pre><code>class Project {
      int projectID;
      String description;
      set&lt;String&gt; clients;
      set&lt;String&gt; employers;
      set&lt;String&gt; technologies;
  }
  </code></pre>
  
  <p>As you can see, each project will have one or more client, employer and technology also. I want to avoid duplication of organization name and technology name across multiple projects. For example, if for some project client is XYZ and for other project also client is XYZ then I just want to refer to same <code>org_id</code> in <code>organization</code> table instead of creating separate rows for same organization.</p>
  
  <p>I was hoping that hibernate will allow me to achieve this easily but I was not able to achieve it using  or with  and  . So I changed my class structure as follows : </p>
  
  <pre><code>class Entity {
      int id;
      String name;
  }
  
  class Client extends Entity {
      // nothing special here
  }
  
  class Employer extends Entity {
      // nothing special here
  }
  
  class Technology extends Entity {
      // nothing special here
  }
  
  class Project {
      int projectID;
      List&lt;Client&gt; clients;
      List&lt;Employer&gt; employers;
      List&lt;Technology&gt; technologies;
  }
  </code></pre>
  
  <p>I have created seperate mapping files for <code>Client</code>,<code>Employer</code> and <code>Technology</code> classes. Even this also has not helped me.</p>