Note that there are some explanatory texts on larger screens.

plurals
  1. POPersisting using hibernate/JPA
    text
    copied!<p>I have been working with hibernate/JPA on JBoss for some months now and have one question that I can't find an answer or solution for.</p> <p>It seems like when creating new entity beans I'm not able to do a query before I at least have called EntityManager.persist(entityBean), or else I get the following error:</p> <p><code>TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing</code></p> <p>An example:</p> <pre><code>Job job = new Job(); Collection&lt;Task&gt; tasks = job.getTasks(); //entityManager.persist(job); ActionPlan actionPlan = (ActionPlan) entityManager.createNamedQuery("ActionPlan.findByCommand"). setParameter("type", RunOperation.Install).getSingleResult(); Task task = Task.getTask(actionPlan); task.setActionPlan(actionPlan); tasks.add(task); task.setJob(job); </code></pre> <p>My problem is that I can't call createNamedQuery without first persisting 'job' (the line that is commented out). ActionPlan has a relation to Job, but the NamedQuery (findByCommand) does not join on Job. What bothers me is that I need to persist Job in order to query the database, when the new created Job is not even interesting in this context.<br> Moving the call to persist() to the end of the snippet yields the above mentioned error.</p> <p>I'm aware that the object I'm working on is not persisted, but persisting makes it impossible to rollback if an error occurs.</p> <p>I believe there is a solution for this, so if someone has the answer I would be very thankful. What am I missing?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload