title: Save new object with Hibernate using supplied ID
body: <p>I want to save some Objects to database with predefined IDs using Hibernate. Is it possible to do it using save method of Hibernate session? </p>
  
  <p>I know there are following workarounds:</p>
  
  <p>1) execute SQL script with necessary insert statements: </p>
  
  <pre><code>insert into MyObj(id,name) values (100,'aaa'), (101,'bbb');
  </code></pre>
  
  <p>2) use SQL query in Hibernate:</p>
  
  <pre><code>public static boolean createObj(Long id, String name) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    if (session.get(MyObj.class, id) == null) {        
      session.beginTransaction();
      SQLQuery sqlQuery = session.createSQLQuery
        ("insert into myobj(id,name) values(?,?)");
      sqlQuery.setLong(0, id);
      sqlQuery.setString(1, name);
      sqlQuery.executeUpdate();
      session.getTransaction().commit();
      return true;
    } else {
      return false;
    }
  }
  </code></pre>
  
  <p>But: is it possible to do without SQLQuery? </p>
  
  <p>In Hibernate reference in section <a href="http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html#objectstate-makingpersistent" rel="noreferrer">11.2. Making objects persistent</a> there is example of code: </p>
  
  <blockquote>
    <p>Alternatively, you can assign the
    identifier using an overloaded version
    of save().</p>
  </blockquote>
  
  <pre><code>DomesticCat pk = new DomesticCat();
  pk.setColor(Color.TABBY);
  pk.setSex('F');
  pk.setName("PK");
  pk.setKittens( new HashSet() );
  pk.addKitten(fritz);
  sess.save( pk, new Long(1234) );
  </code></pre>
  
  <p>But I couldn't find example of how to do this. </p>
  
  <p>So, is it possible to save new objects with supplied IDs to database with Hibernate not using SQL query? If yes then how? 
  And how to overload session method save() as mentioned in Hibernate reference? </p>
  
  <p>Thanks!</p>