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

Reply via email to