It is, indeed. Thanks.
Now I really understand why did the specification declared both "find"
and "getReference". O:)
Regards,
Ognjen
Patrick Linskey wrote:
Hi,
EntityManager.getReference() is your friend here.
-Patrick
On Thu, Apr 3, 2008 at 4:19 AM, Ognjen Blagojevic <[EMAIL PROTECTED]> wrote:
Hi all,
Is there a way to persist an Entity without loading all the referenced
objects?
I know that this is possible:
Employee e = new Employe();
e.setName("John Doe");
e.setIdDepartment(findDepartmentByPrimaryKey(idDept));
e.setIdRank(findRankByPrimaryKey(idRank));
// and so on
em.persist(e);
But if I have a lot of references, and lot of INSERTS to do, this becames
import with poor performace (before each INSERT statemet app must execute N
selects).
It would be much faster if I can set references without looking them up:
Employee e = new Employe();
e.setName("John Doe");
Department dept = new Department();
dept.setIdDepartment(idDept);
e.setIdDepartment(dept);
// and so on
em.persist(e);
(Please note here: I'm not trying to insert new department, but rather to
set a reference to existing one without looking it up in the database.)
But this seems impossible. Without CascadeType.PERSIST it throws something
like:
<openjpa-1.0.2-r420667:627158 nonfatal user error>
org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged
object "[EMAIL PROTECTED]" in persistent field
"mypackage.Employee.idDepartment" of managed object "mypackage.Employee-5"
during flush. However, this field does not allow to be CascadeType.PERSIST.
You cannot flush unmanaged objects.
Ok, I add the CascadeType.PERSIST, and then:
org.apache.openjpa.persistence.InvalidStateException: The generated value
processing detected an existing value assigned to this field:
mypackage.Department.idDepartment. This existing value was either provided
via an initializer or by calling the setter method. You either need to
remove the @GeneratedValue annotation or modify the code to remove the
initializer processing.
It must be a way to INSERT objects in the database without looking up for
all the references, but I am not able to find it.
Regards,
Ognjen