Can not do a striaght to a line update - should be simple

I'm having problems trying to update an existing record in a table - for some reason any, he tries to make an insert (not what I want!) - this is in the meassage of exception, he throws:

RA-01400: cannot insert NULL into ('FOO'. "BAR '." ("' THIS_FIELD")
Error code: 1400

Which is wrong with the code below? :

UOW UnitOfWork = databaseSession.acquireUnitOfWork ();
NewFoo foo = new Foo();
uow.registerNewObject (newFoo);

The ExpressionBuilder builder = new ExpressionBuilder();
Query ReadObjectQuery = new ReadObjectQuery (Foo.class);
query.setSelectionCriteria (builder.getField("FOO_REF").equalsIgnoreCase("564561"));
query.conformResultsInUnitOfWork ();

Foo updateFoo = databaseSession.executeQuery (query) (Foo);
updateFoo.setStatus ("PENDING");
UOW.Commit (); Exception thrown here.

Hello

You call
NewFoo foo = new Foo();
uow.registerNewObject (newFoo);

then
UOW.Commit ();

Everything else is on the databaseSession and unconnected with the UnitOfWork and the transaction. RegisterNewObject will do just that, register the object as if it were a new. In this case, because new Foo() has no identity of the defined object, it is anyway new and would result in an insertion, even if you use RegisterObject() anyway.

Worse still, you then call
updateFoo.setStatus ("PENDING");
who makes changes to the object that you found in the databaseSession - which means that you change the object in the cache.

If all what you intended to do is change the Foo with a '564561' = foo_ref, read from the uow and directly change it instead:

  UnitOfWork uow = databaseSession.acquireUnitOfWork();

  ExpressionBuilder builder = new ExpressionBuilder();
  ReadObjectQuery query = new ReadObjectQuery(Foo.class, builder);
  query.setSelectionCriteria(builder.getField("FOO_REF").equalsIgnoreCase("564561"));
  query.conformResultsInUnitOfWork();

  Foo updateFoo = (Foo) uow.executeQuery(query);//objects returned from the UOW are already registered
  updateFoo.setStatus("PENDING");
  uow.commit(); 

Sure, you could read the databasesession and then call uow.registerObject () or registerExistingObject() on it instead to get the work copy inorder to make your changes.

In addition, if "FOO_REF" has a mapping, you must use.equalsIgnoreCase("564561") instead builder.get("") so that TopLink knows the java field to use to check if the objects are actually still follow in the work unit.

Best regards
Chris

Tags: Fusion Middleware

Similar Questions