surinder-insonix / datanucleus-appengine

Automatically exported from code.google.com/p/datanucleus-appengine
0 stars 0 forks source link

@Unowned fields returned as nulls #315

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

Let's also say I have just 2 @PersistenceCapable classes: Person and Color. 
Every Person has exactly one favourite Color, but it does not mean that this 
Person owns this Color - many different Persons can have the same favourite 
Color. There is a limited number of well-known Colors and a Color may exist 
even if it is not anyone's favourite.

To model this I should use @Unowned relationship - please correct me if I am 
wrong:

@PersistenceCapable
public class Color { // just the most regular Entity class

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    String rgb;

    // getter, setter, constructors
}

@PersistenceCapable
public class Person {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    String surname;

    @Persistent
    @Unowned // here is the tricky part
    Color color;

    // getters, setters, constructors
}

With some simple, well-known, PersistentManager-based code, I am able to 
successfully create and persist an instance of a Color class. I see it in GAE 
Development Console -> Datastore Viewer, having generated Key and ID/Name of 
(13), and my assigned RGB.

With very similar code, I am able to create an instance of Person class (in 
another request), assign a pre-existing Color as his favourite color (it 
pre-existed, I obtained it by pm.getObjectById()) and persist it. I see it in 
Datastore Viewer, with my nice generated Key and ID/Name of (15) and my 
assigned surname, and color_key_OID of (13).

PersistenceManager pm = PMF.get().getPersistenceManager();
Color color = new Color("Blue");
pm.makePersistent(color);
Person person = Person("Surname", color );
pm.makePersistent(person);

What is the expected output? What do you see instead?

But then, when I fetch the Person(15) back from the DB (simple 
pm.getObjectById(), no transactions), it has my assigned surname correctly, but 
has null instead of Color(13)! Right - the Datastore Viewer gets it ok, but my 
code does not.

Person p = pm.getObjectById(Person.class, key);
Color color = p.getColor(); // this returns "null"

What version of the product are you using? On what operating system?
GAE(1.7.7) + GWT(2.5.0) 

Please provide any additional information below.

The datanucleus appengine jar is the following datanucleus-appengine-2.1.2.jar. 
I was reading JDOUnownedOneToOneTest but the classes UnownedJDOOneToOneBiSideA, 
UnownedJDOOneToOneBiSideB, UnownedJDOOneToOneUniSideA and 
UnownedJDOOneToOneUniSideB aren't commited.

Thanks in advance
Juan Manuel

Original issue reported on code.google.com by juanma.v82@gmail.com on 12 Apr 2013 at 6:24

GoogleCodeExporter commented 8 years ago
Aren't committed ? you mean apart from
https://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/go
ogle/appengine/datanucleus/test/jdo/UnownedJDOOneToOneBiSideA.java
https://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/go
ogle/appengine/datanucleus/test/jdo/UnownedJDOOneToOneBiSideB.java
https://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/go
ogle/appengine/datanucleus/test/jdo/UnownedJDOOneToOneUniSideA.java
https://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/go
ogle/appengine/datanucleus/test/jdo/UnownedJDOOneToOneUniSideB.java
which are obviously in public SVN and always have been

Original comment by googleco...@yahoo.co.uk on 14 Apr 2013 at 2:36

GoogleCodeExporter commented 8 years ago
Andy,

Thanks for your response! My mistake, sorry. With the provided links I could 
access to these classes, Thanks! I check my code but keeps falling and still 
returns null.

:-/

Regards
JM

Original comment by juanma.v82@gmail.com on 15 Apr 2013 at 3:22

GoogleCodeExporter commented 8 years ago
Hi again,

Well I was searching a little bit and realized the problem was I did not think 
in the lazy-load, my code was closing the PersistenceManager before getting the 
color member of the object person.

Thanks
Regards
JM

Original comment by juanma.v82@gmail.com on 15 Apr 2013 at 1:40

GoogleCodeExporter commented 8 years ago
Seemingly down to user understanding of fetching process in JDO, so marking as 
not a problem. Obviously provision of a valid testcase to augment the SVN suite 
that demonstrates something could allow this to be reopened.

Original comment by googleco...@yahoo.co.uk on 17 Apr 2013 at 6:47