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.
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:
TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing
An example:
Job job = new Job();
Collection<Task> 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);
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.
Moving the call to persist() to the end of the snippet yields the above mentioned error.
I'm aware that the object I'm working on is not persisted, but persisting makes it impossible to rollback if an error occurs.
I believe there is a solution for this, so if someone has the answer I would be very thankful. What am I missing?