I'd recommend basing your code on the example here to get something working:
http://gae-java-persistence.blogspot.com/2009/10/creating-bidrectional-owned-one-to-many.html

Then you can start tweaking.

Max

On Wed, Dec 30, 2009 at 3:02 PM, markm208 <markm...@gmail.com> wrote:

> I am thoroughly confused… I am trying to set up a simple one to many
> relationship using JPA, I think that this should work but instead I
> get the error "Primary key for object of type Member is null." Any
> help would be greatly appreciated.
>
> @Entity(name = "Member")
> public class Member
> {
>        @Id
>        @GeneratedValue(strategy = GenerationType.IDENTITY)
>        private Key id;
>
>        @Basic //datastore specific type
>        private Email memberEmail;
>        private String memberPassword;
>        private String memberFirstName;
>        private String memberLastName;
>        @Basic //blob of text not searchable
>        private Text memberDescription;
>
>        //Member 'owns' the Searches
>        @OneToMany(mappedBy = "memberWhoCreated", cascade=CascadeType.ALL)
>        private List < Search > searches = new ArrayList < Search >();
>
>        //getters and setters
>        //...
> }
>
> @Entity(name = "Search")
> public class Search
> {
>        @Id
>        @GeneratedValue(strategy = GenerationType.IDENTITY)
>        private Key id;
>
>        private List < String > searchStrings;
>        private boolean allowOthersToSee;
>        @Basic //datastore specific type
>        private Text contextOfSearch;
>
>        @ManyToOne(fetch = FetchType.LAZY)
>        private Member memberWhoCreated;
>
>        public static Search createASearch(boolean allowOthersToSee, String
> context, Member member)
>        {
>                Search search = null;
>
>                EntityManager em = null;
>                try
>                {
>                        //get the entity manager
>                        em = EMF.get().createEntityManager();
>
>                        //create the search from the form variables
>                        search = new Search();
>                        search.setAllowOthersToSee(allowOthersToSee);
>                        search.setContextOfSearch(new Text(context));
>                        search.setMemberWhoCreated(member);
>
>                        em.getTransaction().begin();
>                        try
>                        {
>
>                                //store the member
>                                em.persist(search);
>                                em.getTransaction().commit();
>                        }
>                        finally
>                        {
>                                if (em.getTransaction().isActive())
>                                {
>                                        em.getTransaction().rollback();
>                                }
>                        }
>                }
>                finally
>                {
>                        em.close();
>                }
>
>                return search;
>        }
>
>        //getters and setters
>        //...
> }
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Google App Engine for Java" group.
> To post to this group, send email to
> google-appengine-j...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengine-java+unsubscr...@googlegroups.com<google-appengine-java%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en.
>
>
>

--

You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.


Reply via email to