title: Creating a new object instance in hibernate using a sql query
body: <p>I am attempting to create an object in hibernate using a query, which will then be saved back to the table representing the class.</p>
  
  <p>Excerpt from hbm.xml file:</p>
  
  <pre><code>&lt;class name="MyClass" table="MY_TABLE"&gt;
  &lt;id column="ID" name="ID"&gt;
     &lt;generator class="sequence"&gt;
      &lt;param name="sequence"&gt;MY_SEQ&lt;/param&gt;
     &lt;/generator&gt;
  &lt;/id&gt;
    &lt;property column="VAL" name="val" type="string"/&gt;
  &lt;/class&gt;
  
  &lt;sql-query name="myQuery"&gt;
    &lt;return class="MyClass"/&gt;
    SELECT MY_SEQ.nextval ID, 'something' VAL from DUAL
  &lt;/sql-query&gt;
  </code></pre>
  
  <p>Code snippet from test case:</p>
  
  <pre><code>MyClass myClass = (MyClass) session.getNamedQuery("myQuery").list().get(0);
  Transaction t = session.beginTransaction();
  session.save(myClass);
  t.commit();
  </code></pre>
  
  <p>My aim is that there should now be a new record in table MY_TABLE, but the insert does not occur, I assume that this is due the fact that Hibernate does not know that the instance has not been persisted in the db.</p>
  
  <p>I have tried changing the query to read:</p>
  
  <pre><code>SELECT NULL ID, 'something' VAL from DUAL
  </code></pre>
  
  <p>But this results in Hibernate not instantiating an object.</p>
  
  <p>So how can i create a new object instance from a query that is not associated with a persisted instance of the class and use this to create a persisted instance?</p>