Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Unable to implement update for existing source object to point to existing target object

Hi Mike

Update:

I modified the code a bit to get the Junit test to run:

1. I removed the drop table in tear down so I can see the tables with sqlplus and delete them manually.

2. I added two departments in populate one with id 1010 and the other with id 1011 so that I can see if the modify is working or not.

3. I changed the second argument of helper.addTypes() to true so that a foreign key constraint is generated in the database.

4. I modified the setDept() as follows:

    @Test
    public void setDept() {
        queryTracker.reset();
        EntityManager em = emf.createEntityManager();
        Query query1 = em.createQuery("SELECT e FROM Employee e WHERE e.id = '147'");
        DynamicEntity emp = (DynamicEntity)query1.getSingleResult();

        // modifying the department from 1010 to 1011
        Query query2 = em.createQuery("SELECT d FROM Dept d WHERE d.depid = '1011'");

        DynamicEntity dept = (DynamicEntity)query2.getSingleResult();
        emp.set("empinfo.depinfo", dept);

        // modifying the list of employees as well - the relationship in the opposite direction
        List<DynamicEntity> emps = dept.get("empinfo.depinfo");
        emps.add(emp);

        em.getTransaction().begin();
        em.persist(emp);
        em.getTransaction().commit();
        em.close();
        assertEquals(1, queryTracker.getTotalCalls("UPDATE"));
    }

I have added comments to indicate the changes.
In this case the assert passes. But the foreign key (see point 3 above) in the employee table is not modified. (I can check the tables after the Junit test as I am not dropping them in tear down).

Moreover if I comment the code for adding emp to the list emps the assert fails again.

I also tried printing the queries returned by queryTracker.getQueries() by invoking getSQLString() on each query.
This is what I get:

SELECT id, name, depid FROM emp_empdep WHERE (id = ?)
SELECT depid, depname FROM dep_empdep WHERE (depid = ?)
null

I don't know why the third one is null??? The bottom line is that it is still not working.

5. Just to reiterate, in the original code that I had sent, if I set the LogLevel in the Unit of Work to finest then I observe the following:

    a. If I add the old employee to the list (as in the above code) then I get the following in my logs:
    Execute query UpdateObjectQuery({depinfo 1010})

but this update is not executed against the database.
    b. If I just call the oldEmp.set() and not the emps.add() then I do not get the UpdateObjectQuery in my logs.

    c. If I update the version field for the oldEmp object using the forceUpdateToVersionField I see the following two update queries in the logs. But again no query is executed on the database and the foreign key is not modified.
    Execute query UpdateObjectQuery({depinfo 1010})
    Execute query UpdateObjectQuery({empinfo 147})


Why is the query not executed? How does eclipselink decide whether the query has to be executed or not?

Thanks in Advance
Rohit

On 1/6/2011 1:45 PM, Rohit Banga wrote:
Hi Mike

1. Thanks for the detailed code. But, executing the code on my database I get the following error. The setDept() expects the number of update calls to be 1 but there is no SQL call.

java.lang.AssertionError: expected:<1> but was:<0>
    at org.junit.Assert.fail(Assert.java:91)

Did the JUnit Test case execute correctly in your environment?

2. In the code that I had sent I had not used EntityManagerFactory, JPA queries etc. Which is the preferred/correct way of doing it? I am more comfortable using the Dynamic Persistence APIs of Eclipselink.

Thanks
Rohit


On 1/6/2011 2:15 AM, Michael Norman wrote:
Rohit:
I have put together a simple JUnit4 testsuite that walks thru the lifecycle of building a Dynamic JPA project:

setUp
 - deal with database properties
 - build an EntityManagerFactory: the stuff with MyPersistenceProviderResolver and MyPersistenceProvider
   looks more complicated that is - a Dynamic JPA project doesn't need the persistence.xml and orm.xml files
  This is a way (in JPA2) to override the default PersistenceProvider.

createTypes
 - pretty much the same as how you map the classes

populate
 - (obvious)

setDept
 - sets the 1:1 relationship on an employee to point to the department created in the above populate() method

tearDown
 - drop tables

The assert statements are just checking for the number of SQL statements in each category; if you wish, you could
dig into the tracker's query cache and find the exact strings.

Hope this helps,
---
Oracle
Mike Norman | Principal Software Designer
Phone: +6132884638 | Fax: +6132382818
Server Technologies | EclipseLink Product
Oracle Canada | 45 O'Connor Street, Suite 400, Ottawa, Ontario | K1P 1A4

Hardware
            and Software, Engineered to Work Together
Oracle is
            committed to developing practices and products that help
            protect the environment

On 05/01/2011 1:18 AM, Rohit Banga wrote:
Just to add
I have attached some sample code. In the code I am having a relationship between the employee and phone tables as well but that should not make a difference.

I am using the following documentation:
1. http://wiki.eclipse.org/Using_Basic_Unit_of_Work_API_%28ELUG%29#Associating_an_Existing_Source_to_an_Existing_Target_Object
2. http://wiki.eclipse.org/Using_Advanced_Unit_of_Work_API_%28ELUG%29#How_to_Force_a_Version_Field_Update

The first one talks about the scenario that I am trying to implement. I don't see any significant differences in my implementation. Moreover the second page mentions that
"when a relationship is changed in a one-to-many or one-to-one target foreign key mapping, by default, the version field (if any) of the affected object is not changed"


I am unable to correlate the information contained in the two links. Could you please help me understand it?

Thanks

On 1/5/2011 8:49 AM, Rohit Banga wrote:
Hi Tom,

1. Yes I am invoking commit on UnitOfWork. I have set the log level to FINEST for the UnitOfWork. The SQL does not show up in the logs. The tables are hence not updated. The unit of work is in the end after printing the changes.
If I try to modify any direct to field mapping for the employee then that does show up in the change set. But if eclipselink calculates the changeset piecemeal (does it?), then like you said the change in the the one-to-many mapping may not show up in the changeset.


2. By modification of the list I meant:

List<DynamicEntity> empList = (DynamicEntity) dep.get("empinfo.depinfo");
empList.add(emp);

The introduction of the above code does not work. I am not even sure if the relationship on the other side "must" or "need not" be modified.

3. Yes I have the same mappings. Sorry I did not make it clear in the original question.

Department to Employee - one to many. 
Employee to Department - one to one.
The employee table has a foreign key constraint - depid.
join attribute is "empinfo.depinfo" for both the mappings.

Thanks and Regards
Member Technical Staff 
Oracle Server Technologies
91 80 41085685

----- Original Message -----
From: tom.ware@xxxxxxxxxx
To: rohit.banga@xxxxxxxxxx, eclipselink-users@xxxxxxxxxxx
Sent: Wednesday, January 5, 2011 12:27:21 AM GMT +05:30 Chennai, Kolkata, Mumbai, New Delhi
Subject: Re: [eclipselink-users] Unable to implement update for existing source object to point to existing target object

Hi Rohit,

   Do you actually commit your UnitOfWork?  i.e. are you seeing the problem at 
commit time through the absence of SQL, or are you simply getting the change set 
prior to commit?  If you are getting the change set prior to commit, the changes 
may simply not have been calculated yet.

   What do you mean by: "I have also tried to populate the list corresponding to 
the employees of  department 1010, but even that does not work."

   Please show where your unitOfWork.commit() calls are in the code you have 
included.

   Also, I assume these are the same mappings as you have shown in previous 
postings?


-Tom

Rohit Banga wrote:
  Hi All

I have two tables

1. Employee (id, name, depid)
2. Department (depid, depname)

I have created employee and department dynamic types. I have a OneToMany 
relationship between the department and employee types.

I have to update the department of an employee. I am using the following 
code:

DynamicEntity emp = helper.newDynamicEntity(empBuilder.getType().getName());
emp.set("id", 147);
emp = (DynamicEntity) unitOfWork.readObject(emp);

DynamicEntity dep = helper.newDynamicEntity(depBuilder.getType().getName());
dep.set("depid", 1010);
dep = (DynamicEntity) unitOfWork.readObject(dep);

emp.set("empinfo.depinfo", dep);


However this does not update the depid for the employee row. The change 
is not present in the change set for the unit of work.

The unit of work that I have acquired from the session does not contain 
any objects before the readObject calls in the code above.

*Can you please tell me what is wrong with the above code?*

I have also tried to populate the list corresponding to the employees of 
department 1010, but even that does not work.

*Is it mandatory to update the list or is it sufficient to modify the 
emp object alone?*

When I use forceVersionUpdate for the emp object then, the session log 
does show UpdateQuery in the logs however no update query is executed 
against the database.
*What is the reason for this behavior?*
*
*
*Happy New Year!*
*
*
Thanks
Rohit Banga
Member Technical Staff
Oracle India Private Limited
91 80 41085685


------------------------------------------------------------------------

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

--
Thanks and Regards
Rohit Banga
Member Technical Staff
Oracle Server Technologies
_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users

--
Thanks and Regards
Rohit Banga
Member Technical Staff
Oracle Server Technologies
_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users

--
Thanks and Regards
Rohit Banga
Member Technical Staff
Oracle Server Technologies

Back to the top