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
<[email protected]> 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

Reply via email to