Saving Using Hibernation / JPA

I have been working with hibernate / JPA on JBoss for several months and have one question that I cannot find an answer or solution to.

It seems that when creating a new beans object, I cannot fulfill the request before I even call EntityManager.persist (entityBean), otherwise I will get the following error:

TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing

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);

, createNamedQuery "" (, ). ActionPlan Job, NamedQuery (findByCommand) Job. , Job, , Job .
persist() .

, , , , .

, , , - , . ?

+3
4

, -, , ? . , . , . , - .

P.S. " ".

Session sess = factory.openSession();
Transaction tx;
try {
tx = sess.beginTransaction();
    //do some work
    ...
    tx.commit();
}
catch (Exception e) {
    if (tx!=null) tx.rollback();
    throw e;
}
finally {
    sess.close();
}
+3

, , , - . Entity beans. , Entity beans, . TransientObjectException.

, JBoss? , J2EE JPA , , , ?! , requireNew .., - .

, hibernate, JPA - JBoss . , , .

0

, . flush EntityManager .

0

, , Java EE 5Tutorial .

, persist() , Entity bean persisted. , , , , persist (, , "save" ). , , , - .

, , ; persist , , , , .
, ;)

What remains of her is that I still do not understand why I cannot make a request without everything being in a constant state.

0
source

Source: https://habr.com/ru/post/1699448/


All Articles