You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Christian Grobmeier <gr...@gmail.com> on 2011/09/06 17:14:41 UTC

Getting null instead of the actual value

Folks,

I need your help. An error in my app bugs me and I am looking
desperately were it could happen.

The use case is:
- create a user
- create a user_role object
- select one of the existing roles
- connect all together and commit

I see that all values are correct in the database (mysql console)

But after the user registered, my app fails with the error "no role".

What I do is basically:

ur = User.getUserRoles();
foreach ur:
   r = ur.getRole()


And the r reference is null. I am not sure what I am doing wrong. Any
hints what I could check in my Cayenne app?

I add a role to a user liek this:

User user ...

UserRoles userRoles = new UserRoles();
userRoles.setUser(user);
userRoles.setRole(role);
user.addToUserRoles(userRoles);
role.addToRolesToJoin(userRoles);

Any hints highly apprecitated - thanks in advance

Christian

Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
> One side of prefetching is saving us uneeded queries, when we know upfront which related objects we need in addition to the main query result. This is the angle shown in the docs where prefetching is presented as a performance optimization tool.

yes, thats ok.

> But there is another side of it, which is just as important. It is about fine-grained control of refreshing parts of the object graph already in memory. When a query is run, Cayenne can't automatically refresh all objects directly or indirectly connected to the query result objects, as it would cause a massive fetch. With prefetching Cayenne knows how much of the object graph we want to refresh.

Makes sense.

> So I don't find the result all that surprising. (Although if things happen in the same JVM I am still surprised the relationship isn't refreshed via Cayenne cross-context event mechanism. This could be a race condition actually, as events are delivered asynchronously)

Actually there were no additional events but only two http requests
with 10 seconds between them. No additional ajax. So I can expect two
different contexts, because every request gets its own context -
right?

Can we assume I selected "firstObject" without any prefetching and did
then something like:
firstObject.getSecond().getThird();

I get the second object. But calling second.getThird(); returns null.
Why? Isn't it so that at this place getThird should cause a SQL query?
If this is not the case it still does not explain why it worked from
time to time.

I am really interested in what caused this problem. Thanks guys for
all your patience with me.

Cheers,
Christian


> Andrus
>
>
> On Sep 16, 2011, at 10:01 PM, Christian Grobmeier wrote:
>
>> I have not had duplication issues so far. But I will double check now.
>> I added this code b/c of the problems with the null values, so it isnt
>> the root cause for my problem.
>> At the moment it still looks good. It seems that the prefetch helped.
>> I am very puzzled about it :-)
>>
>> On Fri, Sep 16, 2011 at 8:30 PM, Michael Gentry <mg...@masslight.net> wrote:
>>> I seem to recall doing a setX() followed by an addToY() causes a
>>> duplicate Y to be added.  I'd have to double check this, but that is
>>> my recollection.
>>>
>>> On Fri, Sep 16, 2011 at 2:19 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
>>>>> but may cause duplication issues for addToY().
>>>>
>>>> I hope it doesn't. But since I am trained to just set the rel in one direction, I may not be aware of the issue.
>>>>
>>>>
>>>> On Sep 16, 2011, at 9:12 PM, Mike Kienenberger wrote:
>>>>
>>>>> On Fri, Sep 16, 2011 at 4:22 AM, Christian Grobmeier
>>>>> <gr...@gmail.com> wrote:
>>>>>> userRoles.setUser(user);
>>>>>> userRoles.setRole(role);
>>>>>> user.addToUserRoles(userRoles);
>>>>>> role.addToRolesToJoin(userRoles);
>>>>>
>>>>> Again, unless, you've specifically made both relationships
>>>>> unidirectional (the default is bidirectional), you are actually
>>>>> setting both sides of the relationship twice.   Not an issue for a
>>>>> SetX(), but may cause duplication issues for addToY().
>>>>>
>>>>
>>>>
>>>
>>
>>
>>
>> --
>> http://www.grobmeier.de
>>
>
>



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Andrus Adamchik <an...@objectstyle.org>.
One side of prefetching is saving us uneeded queries, when we know upfront which related objects we need in addition to the main query result. This is the angle shown in the docs where prefetching is presented as a performance optimization tool. 

But there is another side of it, which is just as important. It is about fine-grained control of refreshing parts of the object graph already in memory. When a query is run, Cayenne can't automatically refresh all objects directly or indirectly connected to the query result objects, as it would cause a massive fetch. With prefetching Cayenne knows how much of the object graph we want to refresh.

So I don't find the result all that surprising. (Although if things happen in the same JVM I am still surprised the relationship isn't refreshed via Cayenne cross-context event mechanism. This could be a race condition actually, as events are delivered asynchronously)

Andrus 


On Sep 16, 2011, at 10:01 PM, Christian Grobmeier wrote:

> I have not had duplication issues so far. But I will double check now.
> I added this code b/c of the problems with the null values, so it isnt
> the root cause for my problem.
> At the moment it still looks good. It seems that the prefetch helped.
> I am very puzzled about it :-)
> 
> On Fri, Sep 16, 2011 at 8:30 PM, Michael Gentry <mg...@masslight.net> wrote:
>> I seem to recall doing a setX() followed by an addToY() causes a
>> duplicate Y to be added.  I'd have to double check this, but that is
>> my recollection.
>> 
>> On Fri, Sep 16, 2011 at 2:19 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
>>>> but may cause duplication issues for addToY().
>>> 
>>> I hope it doesn't. But since I am trained to just set the rel in one direction, I may not be aware of the issue.
>>> 
>>> 
>>> On Sep 16, 2011, at 9:12 PM, Mike Kienenberger wrote:
>>> 
>>>> On Fri, Sep 16, 2011 at 4:22 AM, Christian Grobmeier
>>>> <gr...@gmail.com> wrote:
>>>>> userRoles.setUser(user);
>>>>> userRoles.setRole(role);
>>>>> user.addToUserRoles(userRoles);
>>>>> role.addToRolesToJoin(userRoles);
>>>> 
>>>> Again, unless, you've specifically made both relationships
>>>> unidirectional (the default is bidirectional), you are actually
>>>> setting both sides of the relationship twice.   Not an issue for a
>>>> SetX(), but may cause duplication issues for addToY().
>>>> 
>>> 
>>> 
>> 
> 
> 
> 
> -- 
> http://www.grobmeier.de
> 


Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
I have not had duplication issues so far. But I will double check now.
I added this code b/c of the problems with the null values, so it isnt
the root cause for my problem.
At the moment it still looks good. It seems that the prefetch helped.
I am very puzzled about it :-)

On Fri, Sep 16, 2011 at 8:30 PM, Michael Gentry <mg...@masslight.net> wrote:
> I seem to recall doing a setX() followed by an addToY() causes a
> duplicate Y to be added.  I'd have to double check this, but that is
> my recollection.
>
> On Fri, Sep 16, 2011 at 2:19 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
>>> but may cause duplication issues for addToY().
>>
>> I hope it doesn't. But since I am trained to just set the rel in one direction, I may not be aware of the issue.
>>
>>
>> On Sep 16, 2011, at 9:12 PM, Mike Kienenberger wrote:
>>
>>> On Fri, Sep 16, 2011 at 4:22 AM, Christian Grobmeier
>>> <gr...@gmail.com> wrote:
>>>> userRoles.setUser(user);
>>>> userRoles.setRole(role);
>>>> user.addToUserRoles(userRoles);
>>>> role.addToRolesToJoin(userRoles);
>>>
>>> Again, unless, you've specifically made both relationships
>>> unidirectional (the default is bidirectional), you are actually
>>> setting both sides of the relationship twice.   Not an issue for a
>>> SetX(), but may cause duplication issues for addToY().
>>>
>>
>>
>



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Michael Gentry <mg...@masslight.net>.
I seem to recall doing a setX() followed by an addToY() causes a
duplicate Y to be added.  I'd have to double check this, but that is
my recollection.

On Fri, Sep 16, 2011 at 2:19 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
>> but may cause duplication issues for addToY().
>
> I hope it doesn't. But since I am trained to just set the rel in one direction, I may not be aware of the issue.
>
>
> On Sep 16, 2011, at 9:12 PM, Mike Kienenberger wrote:
>
>> On Fri, Sep 16, 2011 at 4:22 AM, Christian Grobmeier
>> <gr...@gmail.com> wrote:
>>> userRoles.setUser(user);
>>> userRoles.setRole(role);
>>> user.addToUserRoles(userRoles);
>>> role.addToRolesToJoin(userRoles);
>>
>> Again, unless, you've specifically made both relationships
>> unidirectional (the default is bidirectional), you are actually
>> setting both sides of the relationship twice.   Not an issue for a
>> SetX(), but may cause duplication issues for addToY().
>>
>
>

Re: Getting null instead of the actual value

Posted by Andrus Adamchik <an...@objectstyle.org>.
> but may cause duplication issues for addToY().

I hope it doesn't. But since I am trained to just set the rel in one direction, I may not be aware of the issue.


On Sep 16, 2011, at 9:12 PM, Mike Kienenberger wrote:

> On Fri, Sep 16, 2011 at 4:22 AM, Christian Grobmeier
> <gr...@gmail.com> wrote:
>> userRoles.setUser(user);
>> userRoles.setRole(role);
>> user.addToUserRoles(userRoles);
>> role.addToRolesToJoin(userRoles);
> 
> Again, unless, you've specifically made both relationships
> unidirectional (the default is bidirectional), you are actually
> setting both sides of the relationship twice.   Not an issue for a
> SetX(), but may cause duplication issues for addToY().
> 


Re: Getting null instead of the actual value

Posted by Mike Kienenberger <mk...@gmail.com>.
On Fri, Sep 16, 2011 at 4:22 AM, Christian Grobmeier
<gr...@gmail.com> wrote:
> userRoles.setUser(user);
> userRoles.setRole(role);
> user.addToUserRoles(userRoles);
> role.addToRolesToJoin(userRoles);

Again, unless, you've specifically made both relationships
unidirectional (the default is bidirectional), you are actually
setting both sides of the relationship twice.   Not an issue for a
SetX(), but may cause duplication issues for addToY().

Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
Hello Andrus,

thanks for your help so far. I have now modfied the code to use a
namedQuery when selecting the user. I have then modeled a prefetch of
the role. I made several tests, all successful for now. I will test
again a little bit later. If it works than my error was somehow
related to the prefetching. If not.... then I will try make more clear
what I have done.

Cheers
Christian

On Fri, Sep 16, 2011 at 12:51 PM, Andrus Adamchik
<an...@objectstyle.org> wrote:
> Hi Christian,
>
> Sorry, I think I am bit lost. Pieces of code shown in this thread don't fall together in one sequence of events in my head, so I can't give specific advice and can only keep guessing. The first message with pseudo code didn't sound right at all.
>
>> But if a make up a new request
>> and select the User, it claims it has no roles which leads to a
>> failure.
>
> Do you prefetch the roles when fetching a user (as in SelectQuery.addPrefetch(..))? That's one possible way to refresh relationships.
>
> Andrus
>
>
>
> On Sep 16, 2011, at 12:40 PM, Christian Grobmeier wrote:
>>>> ObjectContext ctx = // get context
>>>> User user = ctx.newObject(User.class);
>>>> ObjectContext ctx = // get another context (from ThreadLocal - should be the same?)
>>>
>>> If this whole piece of code is from the same request, and the first context is obtained from ThreadLocal, then the contexts should be the same. (A potential race condition - how did the context got into ThreadLocal in the first place? If it comes from the session, multiple request threads can get the same context, if the user clicks quickly or there's lots of AJAX calls on the pages).
>>
>>
>> I use the CayenneFilter in my web.xml. Once a request comes in, it
>> looks like it is the SessionContextRequestHandler who creates my
>> context and binds it to the current thread.
>>
>> Basically it is three steps: register, activate and then login what
>> user does. It fails when the user does his login (sometimes). At this
>> point there is no ajax at all, just plain stuff.
>>
>>
>>
>>
>>>> After your explaination my guess is that I am not allowed to call this:
>>>> BaseContext.getThreadObjectContext();
>>>> twice before I commit. Does this make sense?
>>>
>>> No, you can call getThreadObjectContext as many times as you need. You get the same context within request, which is probably what you want.
>>
>>
>> Yes, exactly.
>>
>>>
>>>
>>>> <query name="getUserRole"
>>>> factory="org.apache.cayenne.map.SelectQueryBuilder" root="obj-entity"
>>>> root-name="Role">
>>>>       <property name="cayenne.SelectQuery.distinct" value="true"/>
>>>>       <property name="cayenne.GenericSelectQuery.cacheStrategy"
>>>> value="SHARED_CACHE"/>
>>>>       <qualifier><![CDATA[name = "user"]]></qualifier>
>>>> </query>
>>>
>>> This query fetches Roles, not UserRoles. From your earlier messages, you were actually looking for UserRoles, and then traversing relationships to Roles from them, no?
>>
>>
>> My "design" is: User <-> UserRoles <-> Roles
>>
>> I have a fixed role  like "user" or "admin" in the roles table. I want
>> to select the existing user role from the table (I do it by the name,
>> which is some kind of key for me) and add it to a user with creating a
>> new UserRoles object.
>>
>> This is actually were is it seems to fail. In my database all three
>> entities are existing and look good. But if a make up a new request
>> and select the User, it claims it has no roles which leads to a
>> failure.
>>
>> My guts say it might also be somehow caching related. When I login i
>> create a simple SelectQuery which selects the User.class by login
>> name. Might it be a good idea to make something like this?
>>
>> SelectQuery query = ...
>> query.setCacheStrategy(QueryCacheStrategy.NO_CACHE);
>>
>> It would give me a very bad taste in my mouth if I would need that.
>>
>> Cheers,
>> Christian
>>
>>
>>> Andrus
>>
>>
>>
>> --
>> http://www.grobmeier.de
>>
>
>



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Andrus Adamchik <an...@objectstyle.org>.
Hi Christian,

Sorry, I think I am bit lost. Pieces of code shown in this thread don't fall together in one sequence of events in my head, so I can't give specific advice and can only keep guessing. The first message with pseudo code didn't sound right at all.

> But if a make up a new request
> and select the User, it claims it has no roles which leads to a
> failure.

Do you prefetch the roles when fetching a user (as in SelectQuery.addPrefetch(..))? That's one possible way to refresh relationships.

Andrus



On Sep 16, 2011, at 12:40 PM, Christian Grobmeier wrote:
>>> ObjectContext ctx = // get context
>>> User user = ctx.newObject(User.class);
>>> ObjectContext ctx = // get another context (from ThreadLocal - should be the same?)
>> 
>> If this whole piece of code is from the same request, and the first context is obtained from ThreadLocal, then the contexts should be the same. (A potential race condition - how did the context got into ThreadLocal in the first place? If it comes from the session, multiple request threads can get the same context, if the user clicks quickly or there's lots of AJAX calls on the pages).
> 
> 
> I use the CayenneFilter in my web.xml. Once a request comes in, it
> looks like it is the SessionContextRequestHandler who creates my
> context and binds it to the current thread.
> 
> Basically it is three steps: register, activate and then login what
> user does. It fails when the user does his login (sometimes). At this
> point there is no ajax at all, just plain stuff.
> 
> 
> 
> 
>>> After your explaination my guess is that I am not allowed to call this:
>>> BaseContext.getThreadObjectContext();
>>> twice before I commit. Does this make sense?
>> 
>> No, you can call getThreadObjectContext as many times as you need. You get the same context within request, which is probably what you want.
> 
> 
> Yes, exactly.
> 
>> 
>> 
>>> <query name="getUserRole"
>>> factory="org.apache.cayenne.map.SelectQueryBuilder" root="obj-entity"
>>> root-name="Role">
>>>       <property name="cayenne.SelectQuery.distinct" value="true"/>
>>>       <property name="cayenne.GenericSelectQuery.cacheStrategy"
>>> value="SHARED_CACHE"/>
>>>       <qualifier><![CDATA[name = "user"]]></qualifier>
>>> </query>
>> 
>> This query fetches Roles, not UserRoles. From your earlier messages, you were actually looking for UserRoles, and then traversing relationships to Roles from them, no?
> 
> 
> My "design" is: User <-> UserRoles <-> Roles
> 
> I have a fixed role  like "user" or "admin" in the roles table. I want
> to select the existing user role from the table (I do it by the name,
> which is some kind of key for me) and add it to a user with creating a
> new UserRoles object.
> 
> This is actually were is it seems to fail. In my database all three
> entities are existing and look good. But if a make up a new request
> and select the User, it claims it has no roles which leads to a
> failure.
> 
> My guts say it might also be somehow caching related. When I login i
> create a simple SelectQuery which selects the User.class by login
> name. Might it be a good idea to make something like this?
> 
> SelectQuery query = ...
> query.setCacheStrategy(QueryCacheStrategy.NO_CACHE);
> 
> It would give me a very bad taste in my mouth if I would need that.
> 
> Cheers,
> Christian
> 
> 
>> Andrus
> 
> 
> 
> -- 
> http://www.grobmeier.de
> 


Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
>> ObjectContext ctx = // get context
>> User user = ctx.newObject(User.class);
>> ObjectContext ctx = // get another context (from ThreadLocal - should be the same?)
>
> If this whole piece of code is from the same request, and the first context is obtained from ThreadLocal, then the contexts should be the same. (A potential race condition - how did the context got into ThreadLocal in the first place? If it comes from the session, multiple request threads can get the same context, if the user clicks quickly or there's lots of AJAX calls on the pages).


I use the CayenneFilter in my web.xml. Once a request comes in, it
looks like it is the SessionContextRequestHandler who creates my
context and binds it to the current thread.

Basically it is three steps: register, activate and then login what
user does. It fails when the user does his login (sometimes). At this
point there is no ajax at all, just plain stuff.




>> After your explaination my guess is that I am not allowed to call this:
>> BaseContext.getThreadObjectContext();
>> twice before I commit. Does this make sense?
>
> No, you can call getThreadObjectContext as many times as you need. You get the same context within request, which is probably what you want.


Yes, exactly.

>
>
>> <query name="getUserRole"
>> factory="org.apache.cayenne.map.SelectQueryBuilder" root="obj-entity"
>> root-name="Role">
>>       <property name="cayenne.SelectQuery.distinct" value="true"/>
>>       <property name="cayenne.GenericSelectQuery.cacheStrategy"
>> value="SHARED_CACHE"/>
>>       <qualifier><![CDATA[name = "user"]]></qualifier>
>> </query>
>
> This query fetches Roles, not UserRoles. From your earlier messages, you were actually looking for UserRoles, and then traversing relationships to Roles from them, no?


My "design" is: User <-> UserRoles <-> Roles

I have a fixed role  like "user" or "admin" in the roles table. I want
to select the existing user role from the table (I do it by the name,
which is some kind of key for me) and add it to a user with creating a
new UserRoles object.

This is actually were is it seems to fail. In my database all three
entities are existing and look good. But if a make up a new request
and select the User, it claims it has no roles which leads to a
failure.

My guts say it might also be somehow caching related. When I login i
create a simple SelectQuery which selects the User.class by login
name. Might it be a good idea to make something like this?

SelectQuery query = ...
query.setCacheStrategy(QueryCacheStrategy.NO_CACHE);

It would give me a very bad taste in my mouth if I would need that.

Cheers,
Christian


> Andrus



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Sep 16, 2011, at 11:22 AM, Christian Grobmeier wrote:

> 
> ObjectContext ctx = // get context
> 
> User user = ctx.newObject(User.class);
> 
> ObjectContext ctx = // get another context (from ThreadLocal - should
> be the same?)

If this whole piece of code is from the same request, and the first context is obtained from ThreadLocal, then the contexts should be the same. (A potential race condition - how did the context got into ThreadLocal in the first place? If it comes from the session, multiple request threads can get the same context, if the user clicks quickly or there's lots of AJAX calls on the pages).

> After your explaination my guess is that I am not allowed to call this:
> BaseContext.getThreadObjectContext();
> twice before I commit. Does this make sense?

No, you can call getThreadObjectContext as many times as you need. You get the same context within request, which is probably what you want.


> <query name="getUserRole"
> factory="org.apache.cayenne.map.SelectQueryBuilder" root="obj-entity"
> root-name="Role">
> 	<property name="cayenne.SelectQuery.distinct" value="true"/>
> 	<property name="cayenne.GenericSelectQuery.cacheStrategy"
> value="SHARED_CACHE"/>
> 	<qualifier><![CDATA[name = "user"]]></qualifier>
> </query>

This query fetches Roles, not UserRoles. From your earlier messages, you were actually looking for UserRoles, and then traversing relationships to Roles from them, no?

Andrus

Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
Hello,

I have now looked on #3. Actually I made something like that:

ObjectContext ctx = // get context

User user = ctx.newObject(User.class);

ObjectContext ctx = // get another context (from ThreadLocal - should
be the same?)
NamedQuery query = new NamedQuery("getUserRole");
List storedUserRoles = ctx.performQuery(query);
Role role = // get the correct role from storedUserRoles

UserRoles userRoles = ctx.newObject(UserRoles.class);
userRoles.setUser(user);
userRoles.setRole(role);
user.addToUserRoles(userRoles);
role.addToRolesToJoin(userRoles);

ctx.commitChanges();


After your explaination my guess is that I am not allowed to call this:
BaseContext.getThreadObjectContext();
twice before I commit. Does this make sense?

I am going to deploy to my test env now and test it a good while.
Hopefully this has helped.

Below is the relevant xml produced by the Cayenne modeler. I have left
out the obj entity stuff. Looks fine to me. In included the named
query.

Thanks and cheers,
Christian

<db-entity name="roles">
	<db-attribute name="id" type="INTEGER" isPrimaryKey="true"
isGenerated="true" isMandatory="true"/>
	…
</db-entity>

<db-entity name="user">
	<db-attribute name="id" type="INTEGER" isPrimaryKey="true"
isGenerated="true" isMandatory="true"/>
	…
</db-entity>

<db-entity name="user_roles">
	<db-attribute name="id" type="INTEGER" isPrimaryKey="true"
isGenerated="true" isMandatory="true"/>
	<db-attribute name="roles_id" type="INTEGER" isMandatory="true"/>
	<db-attribute name="user_id" type="INTEGER" isMandatory="true"/>
</db-entity>
...

<db-relationship name="join_to_role" source="user_roles"
target="roles" toMany="false">
	<db-attribute-pair source="roles_id" target="id"/>
</db-relationship>
<db-relationship name="join_to_user" source="user_roles" target="user"
toMany="false">
	<db-attribute-pair source="user_id" target="id"/>
</db-relationship>
<db-relationship name="user_to_join" source="user" target="user_roles"
toMany="true">
	<db-attribute-pair source="id" target="user_id"/>
</db-relationship>
<db-relationship name="role_to_join" source="roles"
target="user_roles" toMany="true">
	<db-attribute-pair source="id" target="roles_id"/>
</db-relationship>
...

<query name="getUserRole"
factory="org.apache.cayenne.map.SelectQueryBuilder" root="obj-entity"
root-name="Role">
	<property name="cayenne.SelectQuery.distinct" value="true"/>
	<property name="cayenne.GenericSelectQuery.cacheStrategy"
value="SHARED_CACHE"/>
	<qualifier><![CDATA[name = "user"]]></qualifier>
</query>








On Tue, Sep 13, 2011 at 5:24 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
> Then I'd look closer at #3. Something related to what happened in the app between the 2 events.
>
> E.g. one common mistake is using a cross-thread shared ObjectContext (or its objects) to modify and commit objects.
>
> Andrus
>
> On Sep 13, 2011, at 6:20 PM, Christian Grobmeier wrote:
>
>> Hi Andrus,
>>
>> thanks for the comments. I will check that again and come back on
>> thursday with the answer (currently on travel).
>> What bugs me really is that it works sometimes, and sometimes not. Can
>> this behavior be explained with Michaels/your suggestions?
>>
>> Cheers
>>
>> On Tue, Sep 13, 2011 at 4:09 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
>>> I haven't read the entire thread, only the messages quoted below.
>>>
>>> Michael's advise is absolutely correct. However the initial code worked because the Role was likely already registered in the context, so the UserRoles was pulled automatically in the same context.
>>>
>>> So ... a few things to check:
>>>
>>> 1. Can you confirm in DB that there's no userrole records with NULL roleid?
>>> 2. The mapping - if you could post the XML of all 3 DbEntity/ObjEntity mappings with their relationships it may shed some light on this problem.
>>> 3. The context - what happens between commit and access of the role object (serialization/deserialization? Are you working with the same ObjectContext and the same instances of all objects, or are you refetching them fresh? Same ObjectContext or different ones)?
>>>
>>> Andrus
>>>
>>>
>>> On Sep 13, 2011, at 4:47 PM, Christian Grobmeier wrote:
>>>> Hi folks,
>>>>
>>>> i tried this, i didn't work. Any more ideas?
>>>> One thing I should note: it doesn'ht happen all the time
>>>> unfortunately. Its only sometimes and I really don't know why.
>>>>
>>>> More ideas appreciated....
>>>>
>>>> Cheers
>>>> Christian
>>>>
>>>>
>>>>
>>>> On Tue, Sep 6, 2011 at 5:42 PM, Michael Gentry <mg...@masslight.net> wrote:
>>>>> Hi Christian,
>>>>>
>>>>> I suspect it is because you are creating the UserRole outside the
>>>>> DataContext.  Try something like:
>>>>>
>>>>> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
>>>>> userRoles.setUser(user);
>>>>> userRoles.setRole(role);
>>>>>
>>>>> mrg
>>>>>
>>>>>
>>>>> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
>>>>> <gr...@gmail.com> wrote:
>>>>>> Folks,
>>>>>>
>>>>>> I need your help. An error in my app bugs me and I am looking
>>>>>> desperately were it could happen.
>>>>>>
>>>>>> The use case is:
>>>>>> - create a user
>>>>>> - create a user_role object
>>>>>> - select one of the existing roles
>>>>>> - connect all together and commit
>>>>>>
>>>>>> I see that all values are correct in the database (mysql console)
>>>>>>
>>>>>> But after the user registered, my app fails with the error "no role".
>>>>>>
>>>>>> What I do is basically:
>>>>>>
>>>>>> ur = User.getUserRoles();
>>>>>> foreach ur:
>>>>>>   r = ur.getRole()
>>>>>>
>>>>>>
>>>>>> And the r reference is null. I am not sure what I am doing wrong. Any
>>>>>> hints what I could check in my Cayenne app?
>>>>>>
>>>>>> I add a role to a user liek this:
>>>>>>
>>>>>> User user ...
>>>>>>
>>>>>> UserRoles userRoles = new UserRoles();
>>>>>> userRoles.setUser(user);
>>>>>> userRoles.setRole(role);
>>>>>> user.addToUserRoles(userRoles);
>>>>>> role.addToRolesToJoin(userRoles);
>>>>>>
>>>>>> Any hints highly apprecitated - thanks in advance
>>>>>>
>>>>>> Christian
>>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> http://www.grobmeier.de
>>>>
>>>
>>>
>>
>>
>>
>> --
>> http://www.grobmeier.de
>>
>
>



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Andrus Adamchik <an...@objectstyle.org>.
Then I'd look closer at #3. Something related to what happened in the app between the 2 events. 

E.g. one common mistake is using a cross-thread shared ObjectContext (or its objects) to modify and commit objects.

Andrus 

On Sep 13, 2011, at 6:20 PM, Christian Grobmeier wrote:

> Hi Andrus,
> 
> thanks for the comments. I will check that again and come back on
> thursday with the answer (currently on travel).
> What bugs me really is that it works sometimes, and sometimes not. Can
> this behavior be explained with Michaels/your suggestions?
> 
> Cheers
> 
> On Tue, Sep 13, 2011 at 4:09 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
>> I haven't read the entire thread, only the messages quoted below.
>> 
>> Michael's advise is absolutely correct. However the initial code worked because the Role was likely already registered in the context, so the UserRoles was pulled automatically in the same context.
>> 
>> So ... a few things to check:
>> 
>> 1. Can you confirm in DB that there's no userrole records with NULL roleid?
>> 2. The mapping - if you could post the XML of all 3 DbEntity/ObjEntity mappings with their relationships it may shed some light on this problem.
>> 3. The context - what happens between commit and access of the role object (serialization/deserialization? Are you working with the same ObjectContext and the same instances of all objects, or are you refetching them fresh? Same ObjectContext or different ones)?
>> 
>> Andrus
>> 
>> 
>> On Sep 13, 2011, at 4:47 PM, Christian Grobmeier wrote:
>>> Hi folks,
>>> 
>>> i tried this, i didn't work. Any more ideas?
>>> One thing I should note: it doesn'ht happen all the time
>>> unfortunately. Its only sometimes and I really don't know why.
>>> 
>>> More ideas appreciated....
>>> 
>>> Cheers
>>> Christian
>>> 
>>> 
>>> 
>>> On Tue, Sep 6, 2011 at 5:42 PM, Michael Gentry <mg...@masslight.net> wrote:
>>>> Hi Christian,
>>>> 
>>>> I suspect it is because you are creating the UserRole outside the
>>>> DataContext.  Try something like:
>>>> 
>>>> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
>>>> userRoles.setUser(user);
>>>> userRoles.setRole(role);
>>>> 
>>>> mrg
>>>> 
>>>> 
>>>> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
>>>> <gr...@gmail.com> wrote:
>>>>> Folks,
>>>>> 
>>>>> I need your help. An error in my app bugs me and I am looking
>>>>> desperately were it could happen.
>>>>> 
>>>>> The use case is:
>>>>> - create a user
>>>>> - create a user_role object
>>>>> - select one of the existing roles
>>>>> - connect all together and commit
>>>>> 
>>>>> I see that all values are correct in the database (mysql console)
>>>>> 
>>>>> But after the user registered, my app fails with the error "no role".
>>>>> 
>>>>> What I do is basically:
>>>>> 
>>>>> ur = User.getUserRoles();
>>>>> foreach ur:
>>>>>   r = ur.getRole()
>>>>> 
>>>>> 
>>>>> And the r reference is null. I am not sure what I am doing wrong. Any
>>>>> hints what I could check in my Cayenne app?
>>>>> 
>>>>> I add a role to a user liek this:
>>>>> 
>>>>> User user ...
>>>>> 
>>>>> UserRoles userRoles = new UserRoles();
>>>>> userRoles.setUser(user);
>>>>> userRoles.setRole(role);
>>>>> user.addToUserRoles(userRoles);
>>>>> role.addToRolesToJoin(userRoles);
>>>>> 
>>>>> Any hints highly apprecitated - thanks in advance
>>>>> 
>>>>> Christian
>>>>> 
>>>> 
>>> 
>>> 
>>> 
>>> --
>>> http://www.grobmeier.de
>>> 
>> 
>> 
> 
> 
> 
> -- 
> http://www.grobmeier.de
> 


Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
Hi Andrus,

thanks for the comments. I will check that again and come back on
thursday with the answer (currently on travel).
What bugs me really is that it works sometimes, and sometimes not. Can
this behavior be explained with Michaels/your suggestions?

Cheers

On Tue, Sep 13, 2011 at 4:09 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
> I haven't read the entire thread, only the messages quoted below.
>
> Michael's advise is absolutely correct. However the initial code worked because the Role was likely already registered in the context, so the UserRoles was pulled automatically in the same context.
>
> So ... a few things to check:
>
> 1. Can you confirm in DB that there's no userrole records with NULL roleid?
> 2. The mapping - if you could post the XML of all 3 DbEntity/ObjEntity mappings with their relationships it may shed some light on this problem.
> 3. The context - what happens between commit and access of the role object (serialization/deserialization? Are you working with the same ObjectContext and the same instances of all objects, or are you refetching them fresh? Same ObjectContext or different ones)?
>
> Andrus
>
>
> On Sep 13, 2011, at 4:47 PM, Christian Grobmeier wrote:
>> Hi folks,
>>
>> i tried this, i didn't work. Any more ideas?
>> One thing I should note: it doesn'ht happen all the time
>> unfortunately. Its only sometimes and I really don't know why.
>>
>> More ideas appreciated....
>>
>> Cheers
>> Christian
>>
>>
>>
>> On Tue, Sep 6, 2011 at 5:42 PM, Michael Gentry <mg...@masslight.net> wrote:
>>> Hi Christian,
>>>
>>> I suspect it is because you are creating the UserRole outside the
>>> DataContext.  Try something like:
>>>
>>> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
>>> userRoles.setUser(user);
>>> userRoles.setRole(role);
>>>
>>> mrg
>>>
>>>
>>> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
>>> <gr...@gmail.com> wrote:
>>>> Folks,
>>>>
>>>> I need your help. An error in my app bugs me and I am looking
>>>> desperately were it could happen.
>>>>
>>>> The use case is:
>>>> - create a user
>>>> - create a user_role object
>>>> - select one of the existing roles
>>>> - connect all together and commit
>>>>
>>>> I see that all values are correct in the database (mysql console)
>>>>
>>>> But after the user registered, my app fails with the error "no role".
>>>>
>>>> What I do is basically:
>>>>
>>>> ur = User.getUserRoles();
>>>> foreach ur:
>>>>   r = ur.getRole()
>>>>
>>>>
>>>> And the r reference is null. I am not sure what I am doing wrong. Any
>>>> hints what I could check in my Cayenne app?
>>>>
>>>> I add a role to a user liek this:
>>>>
>>>> User user ...
>>>>
>>>> UserRoles userRoles = new UserRoles();
>>>> userRoles.setUser(user);
>>>> userRoles.setRole(role);
>>>> user.addToUserRoles(userRoles);
>>>> role.addToRolesToJoin(userRoles);
>>>>
>>>> Any hints highly apprecitated - thanks in advance
>>>>
>>>> Christian
>>>>
>>>
>>
>>
>>
>> --
>> http://www.grobmeier.de
>>
>
>



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Andrus Adamchik <an...@objectstyle.org>.
I haven't read the entire thread, only the messages quoted below. 

Michael's advise is absolutely correct. However the initial code worked because the Role was likely already registered in the context, so the UserRoles was pulled automatically in the same context.

So ... a few things to check:

1. Can you confirm in DB that there's no userrole records with NULL roleid?
2. The mapping - if you could post the XML of all 3 DbEntity/ObjEntity mappings with their relationships it may shed some light on this problem.
3. The context - what happens between commit and access of the role object (serialization/deserialization? Are you working with the same ObjectContext and the same instances of all objects, or are you refetching them fresh? Same ObjectContext or different ones)?

Andrus


On Sep 13, 2011, at 4:47 PM, Christian Grobmeier wrote:
> Hi folks,
> 
> i tried this, i didn't work. Any more ideas?
> One thing I should note: it doesn'ht happen all the time
> unfortunately. Its only sometimes and I really don't know why.
> 
> More ideas appreciated....
> 
> Cheers
> Christian
> 
> 
> 
> On Tue, Sep 6, 2011 at 5:42 PM, Michael Gentry <mg...@masslight.net> wrote:
>> Hi Christian,
>> 
>> I suspect it is because you are creating the UserRole outside the
>> DataContext.  Try something like:
>> 
>> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
>> userRoles.setUser(user);
>> userRoles.setRole(role);
>> 
>> mrg
>> 
>> 
>> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
>> <gr...@gmail.com> wrote:
>>> Folks,
>>> 
>>> I need your help. An error in my app bugs me and I am looking
>>> desperately were it could happen.
>>> 
>>> The use case is:
>>> - create a user
>>> - create a user_role object
>>> - select one of the existing roles
>>> - connect all together and commit
>>> 
>>> I see that all values are correct in the database (mysql console)
>>> 
>>> But after the user registered, my app fails with the error "no role".
>>> 
>>> What I do is basically:
>>> 
>>> ur = User.getUserRoles();
>>> foreach ur:
>>>   r = ur.getRole()
>>> 
>>> 
>>> And the r reference is null. I am not sure what I am doing wrong. Any
>>> hints what I could check in my Cayenne app?
>>> 
>>> I add a role to a user liek this:
>>> 
>>> User user ...
>>> 
>>> UserRoles userRoles = new UserRoles();
>>> userRoles.setUser(user);
>>> userRoles.setRole(role);
>>> user.addToUserRoles(userRoles);
>>> role.addToRolesToJoin(userRoles);
>>> 
>>> Any hints highly apprecitated - thanks in advance
>>> 
>>> Christian
>>> 
>> 
> 
> 
> 
> -- 
> http://www.grobmeier.de
> 


Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
Hi folks,

i tried this, i didn't work. Any more ideas?
One thing I should note: it doesn'ht happen all the time
unfortunately. Its only sometimes and I really don't know why.

More ideas appreciated....

Cheers
Christian



On Tue, Sep 6, 2011 at 5:42 PM, Michael Gentry <mg...@masslight.net> wrote:
> Hi Christian,
>
> I suspect it is because you are creating the UserRole outside the
> DataContext.  Try something like:
>
> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
> userRoles.setUser(user);
> userRoles.setRole(role);
>
> mrg
>
>
> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
> <gr...@gmail.com> wrote:
>> Folks,
>>
>> I need your help. An error in my app bugs me and I am looking
>> desperately were it could happen.
>>
>> The use case is:
>> - create a user
>> - create a user_role object
>> - select one of the existing roles
>> - connect all together and commit
>>
>> I see that all values are correct in the database (mysql console)
>>
>> But after the user registered, my app fails with the error "no role".
>>
>> What I do is basically:
>>
>> ur = User.getUserRoles();
>> foreach ur:
>>   r = ur.getRole()
>>
>>
>> And the r reference is null. I am not sure what I am doing wrong. Any
>> hints what I could check in my Cayenne app?
>>
>> I add a role to a user liek this:
>>
>> User user ...
>>
>> UserRoles userRoles = new UserRoles();
>> userRoles.setUser(user);
>> userRoles.setRole(role);
>> user.addToUserRoles(userRoles);
>> role.addToRolesToJoin(userRoles);
>>
>> Any hints highly apprecitated - thanks in advance
>>
>> Christian
>>
>



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
> I assumed Christian was calling commitChanges() and was just showing a
> smaller snippet of code.  Perhaps my assumption there was incorrect,
> though.

No it was correct - i tried to make it readable and as easy to
understand as possible

>
> Thanks,
>
> mrg
>
>
> On Tue, Sep 6, 2011 at 12:20 PM, Joe Baldwin <jf...@earthlink.net> wrote:
>> Michael,
>>
>> I thought that you need to commit changes on the ObjectContext at some point or else the new object will not be persisted.   Is this step not needed?
>>
>> Joe
>>
>>
>>
>> On Sep 6, 2011, at 11:42 AM, Michael Gentry wrote:
>>
>>> Hi Christian,
>>>
>>> I suspect it is because you are creating the UserRole outside the
>>> DataContext.  Try something like:
>>>
>>> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
>>> userRoles.setUser(user);
>>> userRoles.setRole(role);
>>>
>>> mrg
>>>
>>>
>>> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
>>> <gr...@gmail.com> wrote:
>>>> Folks,
>>>>
>>>> I need your help. An error in my app bugs me and I am looking
>>>> desperately were it could happen.
>>>>
>>>> The use case is:
>>>> - create a user
>>>> - create a user_role object
>>>> - select one of the existing roles
>>>> - connect all together and commit
>>>>
>>>> I see that all values are correct in the database (mysql console)
>>>>
>>>> But after the user registered, my app fails with the error "no role".
>>>>
>>>> What I do is basically:
>>>>
>>>> ur = User.getUserRoles();
>>>> foreach ur:
>>>>   r = ur.getRole()
>>>>
>>>>
>>>> And the r reference is null. I am not sure what I am doing wrong. Any
>>>> hints what I could check in my Cayenne app?
>>>>
>>>> I add a role to a user liek this:
>>>>
>>>> User user ...
>>>>
>>>> UserRoles userRoles = new UserRoles();
>>>> userRoles.setUser(user);
>>>> userRoles.setRole(role);
>>>> user.addToUserRoles(userRoles);
>>>> role.addToRolesToJoin(userRoles);
>>>>
>>>> Any hints highly apprecitated - thanks in advance
>>>>
>>>> Christian
>>>>
>>
>>
>



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Michael Gentry <mg...@masslight.net>.
Hi Joe,

I assumed Christian was calling commitChanges() and was just showing a
smaller snippet of code.  Perhaps my assumption there was incorrect,
though.

Thanks,

mrg


On Tue, Sep 6, 2011 at 12:20 PM, Joe Baldwin <jf...@earthlink.net> wrote:
> Michael,
>
> I thought that you need to commit changes on the ObjectContext at some point or else the new object will not be persisted.   Is this step not needed?
>
> Joe
>
>
>
> On Sep 6, 2011, at 11:42 AM, Michael Gentry wrote:
>
>> Hi Christian,
>>
>> I suspect it is because you are creating the UserRole outside the
>> DataContext.  Try something like:
>>
>> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
>> userRoles.setUser(user);
>> userRoles.setRole(role);
>>
>> mrg
>>
>>
>> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
>> <gr...@gmail.com> wrote:
>>> Folks,
>>>
>>> I need your help. An error in my app bugs me and I am looking
>>> desperately were it could happen.
>>>
>>> The use case is:
>>> - create a user
>>> - create a user_role object
>>> - select one of the existing roles
>>> - connect all together and commit
>>>
>>> I see that all values are correct in the database (mysql console)
>>>
>>> But after the user registered, my app fails with the error "no role".
>>>
>>> What I do is basically:
>>>
>>> ur = User.getUserRoles();
>>> foreach ur:
>>>   r = ur.getRole()
>>>
>>>
>>> And the r reference is null. I am not sure what I am doing wrong. Any
>>> hints what I could check in my Cayenne app?
>>>
>>> I add a role to a user liek this:
>>>
>>> User user ...
>>>
>>> UserRoles userRoles = new UserRoles();
>>> userRoles.setUser(user);
>>> userRoles.setRole(role);
>>> user.addToUserRoles(userRoles);
>>> role.addToRolesToJoin(userRoles);
>>>
>>> Any hints highly apprecitated - thanks in advance
>>>
>>> Christian
>>>
>
>

Re: Getting null instead of the actual value

Posted by Joe Baldwin <jf...@earthlink.net>.
Michael,

I thought that you need to commit changes on the ObjectContext at some point or else the new object will not be persisted.   Is this step not needed?

Joe



On Sep 6, 2011, at 11:42 AM, Michael Gentry wrote:

> Hi Christian,
> 
> I suspect it is because you are creating the UserRole outside the
> DataContext.  Try something like:
> 
> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
> userRoles.setUser(user);
> userRoles.setRole(role);
> 
> mrg
> 
> 
> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
> <gr...@gmail.com> wrote:
>> Folks,
>> 
>> I need your help. An error in my app bugs me and I am looking
>> desperately were it could happen.
>> 
>> The use case is:
>> - create a user
>> - create a user_role object
>> - select one of the existing roles
>> - connect all together and commit
>> 
>> I see that all values are correct in the database (mysql console)
>> 
>> But after the user registered, my app fails with the error "no role".
>> 
>> What I do is basically:
>> 
>> ur = User.getUserRoles();
>> foreach ur:
>>   r = ur.getRole()
>> 
>> 
>> And the r reference is null. I am not sure what I am doing wrong. Any
>> hints what I could check in my Cayenne app?
>> 
>> I add a role to a user liek this:
>> 
>> User user ...
>> 
>> UserRoles userRoles = new UserRoles();
>> userRoles.setUser(user);
>> userRoles.setRole(role);
>> user.addToUserRoles(userRoles);
>> role.addToRolesToJoin(userRoles);
>> 
>> Any hints highly apprecitated - thanks in advance
>> 
>> Christian
>> 


Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
On Tue, Sep 6, 2011 at 6:55 PM, Mike Kienenberger <mk...@gmail.com> wrote:
> It's probably also worth noting that, as MIchael's example shows, you
> do not need to set both sides of the relationship.

OK.

I have done this while investigating my error - I thought this might
be the case (like I had once in Hibernate or Toplink world)


>
> On Tue, Sep 6, 2011 at 12:46 PM, Christian Grobmeier
> <gr...@gmail.com> wrote:
>> Hi Michael,
>>
>>> I suspect it is because you are creating the UserRole outside the
>>> DataContext.  Try something like:
>>>
>>> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
>>> userRoles.setUser(user);
>>> userRoles.setRole(role);
>>
>> actually this might be the case - I will test this. This would explain
>> why it gets into the database but is missed in the context
>>
>> Cheers
>>
>>>
>>> mrg
>>>
>>>
>>> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
>>> <gr...@gmail.com> wrote:
>>>> Folks,
>>>>
>>>> I need your help. An error in my app bugs me and I am looking
>>>> desperately were it could happen.
>>>>
>>>> The use case is:
>>>> - create a user
>>>> - create a user_role object
>>>> - select one of the existing roles
>>>> - connect all together and commit
>>>>
>>>> I see that all values are correct in the database (mysql console)
>>>>
>>>> But after the user registered, my app fails with the error "no role".
>>>>
>>>> What I do is basically:
>>>>
>>>> ur = User.getUserRoles();
>>>> foreach ur:
>>>>   r = ur.getRole()
>>>>
>>>>
>>>> And the r reference is null. I am not sure what I am doing wrong. Any
>>>> hints what I could check in my Cayenne app?
>>>>
>>>> I add a role to a user liek this:
>>>>
>>>> User user ...
>>>>
>>>> UserRoles userRoles = new UserRoles();
>>>> userRoles.setUser(user);
>>>> userRoles.setRole(role);
>>>> user.addToUserRoles(userRoles);
>>>> role.addToRolesToJoin(userRoles);
>>>>
>>>> Any hints highly apprecitated - thanks in advance
>>>>
>>>> Christian
>>>>
>>>
>>
>>
>>
>> --
>> http://www.grobmeier.de
>>
>



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Mike Kienenberger <mk...@gmail.com>.
It's probably also worth noting that, as MIchael's example shows, you
do not need to set both sides of the relationship.

On Tue, Sep 6, 2011 at 12:46 PM, Christian Grobmeier
<gr...@gmail.com> wrote:
> Hi Michael,
>
>> I suspect it is because you are creating the UserRole outside the
>> DataContext.  Try something like:
>>
>> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
>> userRoles.setUser(user);
>> userRoles.setRole(role);
>
> actually this might be the case - I will test this. This would explain
> why it gets into the database but is missed in the context
>
> Cheers
>
>>
>> mrg
>>
>>
>> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
>> <gr...@gmail.com> wrote:
>>> Folks,
>>>
>>> I need your help. An error in my app bugs me and I am looking
>>> desperately were it could happen.
>>>
>>> The use case is:
>>> - create a user
>>> - create a user_role object
>>> - select one of the existing roles
>>> - connect all together and commit
>>>
>>> I see that all values are correct in the database (mysql console)
>>>
>>> But after the user registered, my app fails with the error "no role".
>>>
>>> What I do is basically:
>>>
>>> ur = User.getUserRoles();
>>> foreach ur:
>>>   r = ur.getRole()
>>>
>>>
>>> And the r reference is null. I am not sure what I am doing wrong. Any
>>> hints what I could check in my Cayenne app?
>>>
>>> I add a role to a user liek this:
>>>
>>> User user ...
>>>
>>> UserRoles userRoles = new UserRoles();
>>> userRoles.setUser(user);
>>> userRoles.setRole(role);
>>> user.addToUserRoles(userRoles);
>>> role.addToRolesToJoin(userRoles);
>>>
>>> Any hints highly apprecitated - thanks in advance
>>>
>>> Christian
>>>
>>
>
>
>
> --
> http://www.grobmeier.de
>

Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
Hi Michael,

> I suspect it is because you are creating the UserRole outside the
> DataContext.  Try something like:
>
> UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
> userRoles.setUser(user);
> userRoles.setRole(role);

actually this might be the case - I will test this. This would explain
why it gets into the database but is missed in the context

Cheers

>
> mrg
>
>
> On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
> <gr...@gmail.com> wrote:
>> Folks,
>>
>> I need your help. An error in my app bugs me and I am looking
>> desperately were it could happen.
>>
>> The use case is:
>> - create a user
>> - create a user_role object
>> - select one of the existing roles
>> - connect all together and commit
>>
>> I see that all values are correct in the database (mysql console)
>>
>> But after the user registered, my app fails with the error "no role".
>>
>> What I do is basically:
>>
>> ur = User.getUserRoles();
>> foreach ur:
>>   r = ur.getRole()
>>
>>
>> And the r reference is null. I am not sure what I am doing wrong. Any
>> hints what I could check in my Cayenne app?
>>
>> I add a role to a user liek this:
>>
>> User user ...
>>
>> UserRoles userRoles = new UserRoles();
>> userRoles.setUser(user);
>> userRoles.setRole(role);
>> user.addToUserRoles(userRoles);
>> role.addToRolesToJoin(userRoles);
>>
>> Any hints highly apprecitated - thanks in advance
>>
>> Christian
>>
>



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Michael Gentry <mg...@masslight.net>.
Hi Christian,

I suspect it is because you are creating the UserRole outside the
DataContext.  Try something like:

UserRoles userRoles = user.getObjectContext.newObject(UserRoles.class);
userRoles.setUser(user);
userRoles.setRole(role);

mrg


On Tue, Sep 6, 2011 at 11:14 AM, Christian Grobmeier
<gr...@gmail.com> wrote:
> Folks,
>
> I need your help. An error in my app bugs me and I am looking
> desperately were it could happen.
>
> The use case is:
> - create a user
> - create a user_role object
> - select one of the existing roles
> - connect all together and commit
>
> I see that all values are correct in the database (mysql console)
>
> But after the user registered, my app fails with the error "no role".
>
> What I do is basically:
>
> ur = User.getUserRoles();
> foreach ur:
>   r = ur.getRole()
>
>
> And the r reference is null. I am not sure what I am doing wrong. Any
> hints what I could check in my Cayenne app?
>
> I add a role to a user liek this:
>
> User user ...
>
> UserRoles userRoles = new UserRoles();
> userRoles.setUser(user);
> userRoles.setRole(role);
> user.addToUserRoles(userRoles);
> role.addToRolesToJoin(userRoles);
>
> Any hints highly apprecitated - thanks in advance
>
> Christian
>

Re: Getting null instead of the actual value

Posted by Joe Baldwin <jf...@earthlink.net>.
Christian

BTW, the "totally easy" was not a smart-alec remark. :)

I think it is essential to note that you have to do 3 steps: get your ObjectContext, create the new entity using the "newObject" method, and then eventually "commitChanges".  My experience has been that if you do not do these three steps then the "new object" will disappear (even though it looks like it is persisted, it may only be an illusion).

Joe




On Sep 6, 2011, at 12:44 PM, Christian Grobmeier wrote:

> Joe,
> 
>> It is a tad difficult to follow all of your pseudo-code (as with all pseudo-code), but it appears that you are adding to "UserRoles" object via Cayenne and that you have left out the step to "commitChanges" on your ObjectContext.
>> 
>> Here is the basic pseudo code for Cayenne usage model:
>> 
>> 1. get your ObjectContext (ex: BaseContext.getThreadObjectContext();)
>> 2. create a new entity on the OC (ex: (UserRoles)oc.newObject(UserRoles.class);)
>> 3. initialize your entity with values
>> 4. commit changes (ex: oc.commitChanges();)
>> 
>> Totally easy! :)
> 
> hehehe I agree it is totally easy and I must say I have called
> commitChanges but it didn't work - thats why I felt so dumb :-)
> 
> Thanks for looking in my hard to read example - I already see another
> valuable tipp in the thread
> 
> Cheers
> 
>> 
>> Joe
>> 
>> 
>> 
>> On Sep 6, 2011, at 11:14 AM, Christian Grobmeier wrote:
>> 
>>> Folks,
>>> 
>>> I need your help. An error in my app bugs me and I am looking
>>> desperately were it could happen.
>>> 
>>> The use case is:
>>> - create a user
>>> - create a user_role object
>>> - select one of the existing roles
>>> - connect all together and commit
>>> 
>>> I see that all values are correct in the database (mysql console)
>>> 
>>> But after the user registered, my app fails with the error "no role".
>>> 
>>> What I do is basically:
>>> 
>>> ur = User.getUserRoles();
>>> foreach ur:
>>>   r = ur.getRole()
>>> 
>>> 
>>> And the r reference is null. I am not sure what I am doing wrong. Any
>>> hints what I could check in my Cayenne app?
>>> 
>>> I add a role to a user liek this:
>>> 
>>> User user ...
>>> 
>>> UserRoles userRoles = new UserRoles();
>>> userRoles.setUser(user);
>>> userRoles.setRole(role);
>>> user.addToUserRoles(userRoles);
>>> role.addToRolesToJoin(userRoles);
>>> 
>>> Any hints highly apprecitated - thanks in advance
>>> 
>>> Christian
>> 
>> 
> 
> 
> 
> -- 
> http://www.grobmeier.de


Re: Getting null instead of the actual value

Posted by Christian Grobmeier <gr...@gmail.com>.
Joe,

> It is a tad difficult to follow all of your pseudo-code (as with all pseudo-code), but it appears that you are adding to "UserRoles" object via Cayenne and that you have left out the step to "commitChanges" on your ObjectContext.
>
> Here is the basic pseudo code for Cayenne usage model:
>
> 1. get your ObjectContext (ex: BaseContext.getThreadObjectContext();)
> 2. create a new entity on the OC (ex: (UserRoles)oc.newObject(UserRoles.class);)
> 3. initialize your entity with values
> 4. commit changes (ex: oc.commitChanges();)
>
> Totally easy! :)

hehehe I agree it is totally easy and I must say I have called
commitChanges but it didn't work - thats why I felt so dumb :-)

Thanks for looking in my hard to read example - I already see another
valuable tipp in the thread

Cheers

>
> Joe
>
>
>
> On Sep 6, 2011, at 11:14 AM, Christian Grobmeier wrote:
>
>> Folks,
>>
>> I need your help. An error in my app bugs me and I am looking
>> desperately were it could happen.
>>
>> The use case is:
>> - create a user
>> - create a user_role object
>> - select one of the existing roles
>> - connect all together and commit
>>
>> I see that all values are correct in the database (mysql console)
>>
>> But after the user registered, my app fails with the error "no role".
>>
>> What I do is basically:
>>
>> ur = User.getUserRoles();
>> foreach ur:
>>   r = ur.getRole()
>>
>>
>> And the r reference is null. I am not sure what I am doing wrong. Any
>> hints what I could check in my Cayenne app?
>>
>> I add a role to a user liek this:
>>
>> User user ...
>>
>> UserRoles userRoles = new UserRoles();
>> userRoles.setUser(user);
>> userRoles.setRole(role);
>> user.addToUserRoles(userRoles);
>> role.addToRolesToJoin(userRoles);
>>
>> Any hints highly apprecitated - thanks in advance
>>
>> Christian
>
>



-- 
http://www.grobmeier.de

Re: Getting null instead of the actual value

Posted by Joe Baldwin <jf...@earthlink.net>.
Christian,

It is a tad difficult to follow all of your pseudo-code (as with all pseudo-code), but it appears that you are adding to "UserRoles" object via Cayenne and that you have left out the step to "commitChanges" on your ObjectContext.

Here is the basic pseudo code for Cayenne usage model:

1. get your ObjectContext (ex: BaseContext.getThreadObjectContext();)
2. create a new entity on the OC (ex: (UserRoles)oc.newObject(UserRoles.class);)
3. initialize your entity with values
4. commit changes (ex: oc.commitChanges();)

Totally easy! :)

Joe



On Sep 6, 2011, at 11:14 AM, Christian Grobmeier wrote:

> Folks,
> 
> I need your help. An error in my app bugs me and I am looking
> desperately were it could happen.
> 
> The use case is:
> - create a user
> - create a user_role object
> - select one of the existing roles
> - connect all together and commit
> 
> I see that all values are correct in the database (mysql console)
> 
> But after the user registered, my app fails with the error "no role".
> 
> What I do is basically:
> 
> ur = User.getUserRoles();
> foreach ur:
>   r = ur.getRole()
> 
> 
> And the r reference is null. I am not sure what I am doing wrong. Any
> hints what I could check in my Cayenne app?
> 
> I add a role to a user liek this:
> 
> User user ...
> 
> UserRoles userRoles = new UserRoles();
> userRoles.setUser(user);
> userRoles.setRole(role);
> user.addToUserRoles(userRoles);
> role.addToRolesToJoin(userRoles);
> 
> Any hints highly apprecitated - thanks in advance
> 
> Christian