AK <ama-l...@...> writes: 
> I have two questions:
> 
> I have two model objects, Item and Person that have a one-to-one 
> relationship driven by the primary key.  Each of these objects extend a 
> ModelBase object that has a generated ID (annotations: @Id and 
> @GeneratedValue).
> 
> Am I correct to relate these objects to one another like this?
> 
>       @Entity
>       public class Item extends ModelBase {
> 
>               @OneToOne @PrimaryKeyJoinColumn
>               private Person person;
>               ...
>               // getters and setters
>       }
> 
>       @Entity
>       public class Person extends ModelBase {
> 
>               @OneToOne
>               private Item item;
>               ...
>               // getters and setters
>       }
> 
> Furthermore, how do I assign values to these in my action class when 
> populating via a form that assigns values to attributes in each model? 
> Right now, I have an action bean that is supposed to populate both, but on 
> persistence, my Person object is always null:
> 
> public class ItemFormActionBean extends BaseActionBean {
> 
>       @ValidateNestedProperties ({
>               @Validate(field="name", required=true, on="submit"),
>               @Validate(field="desc", required=true, on="submit"),
>       })
>       private Item item;
> 
>       @ValidateNestedProperties ({
>               @Validate(field="eMail", required=true, on="submit",
>                               converter=EmailTypeConverter.class)
>       })
>       private Person person;
>       ...
>       // getters and setters
> 
>       public Resolution submit() {
> 
>               EntityManager em = Stripersist.getEntityManager();
> 
>               em.persist(item);
>               em.persist(person);
>               em.getTransaction().commit();
>               ...
>       }
>       ...
> }

I'm following up on this post for anyone that might be running into the same 
issues.  I got some great help on the IRC channel, and thanks to those that 
helped resolved this, espescially aporter.

According to the 2nd example in the OneToOne JavaDoc (http://java.sun.com/
products/persistence/javadoc-1_0-fr/javax/persistence/OneToOne.html) I had the 
Item model contain a Person model but not the other way around:

    @Entity
    public class Item extends ModelBase {
        @OneToOne
        private Person person;
        ...
     }

    @Entity
    public class Person extends ModelBase {
        // no reference to Item
        ...
    }

Secondly, because of this relationship you have to act on the Person model /
through/ Item.  In the JSP:

....
<tr>    <!-- *** access person's email field through item *** -->
    <td><s:label for="item.person.email">E-mail:</s:label></td>
    <td><s:text name="item.person.email" class="required"/></td>
    <td><s:errors field="item.person.email"/></td>
</tr>
<tr>
    <td><s:label for="item.name">Item Found:</s:label></td>
    <td><s:text name="item.name" class="required"/></td>
    <td><s:errors field="item.name"/></td>
</tr>
...

In the action bean for validation:

...
public class ItemFormActionBean extends BaseActionBean {
        @ValidateNestedProperties ({
                @Validate(field="person.email", required=true, on="addItem",
                                converter=EmailTypeConverter.class),
                @Validate(field="name", required=true, on="addItem"),
                @Validate(field="desc", required=true, on="addItem"),
                @Validate(field="dateFound", required=false, on="addItem"),
                @Validate(field="stateId", required=true, on="addItem"),
                @Validate(field="cityId", required=true, on="addItem")
        })
        private Item item;
        ...

...and for persistence:

public Resolution addItem() {
    EntityManager em = Stripersist.getEntityManager();
    em.persist(item);
    
        // persist Person *in* Item
    em.persist(item.getPerson());
    em.getTransaction().commit();
    ...

Hope this helps.


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to