[appengine-java] Object references imply ownership?

2010-10-04 Thread Dave Hicks
 I'm using JPA, and I'm having a really tough time getting around this 
problem.  It seems that when I try to refer to an object by way of a 
@ManyToOne, somehow DataNucleus wants to assume some kind of ownership 
and throws an exception.


I have three kinds of objects:  Person, Book, Rating.  Person and Book 
objects are independent of one another and created independently.  When 
a person rates a Book, a new Rating object is created and added to a set 
of Ratings owned by the Person.  The Rating object has a reference to 
the Book.  Here are the classes (each extends a superclass that contains 
the @Id element):


class Person extends BasicEntity {
private String email;

@OneToMany(mappedBy=person, cascade=CascadeType.All, 
fetch=FetchType.LAZY)

private SetRating ratings;
}

class Book extends BasicEntity {
private String name;
}

class Rating extends BasicEntity {
@ManyToOne(fetch=FetchType.LAZY)
private Person person;

@ManyToOne(fetch=FetchType.LAZY)
private Book book;

private Integer value;
}

I get the following exception when I attempt to add such a Rating to a 
Person:
org.springframework.orm.jpa.JpaSystemException: Detected attempt to 
establish Person(1)/Rating(3) as the parent of Book(2) but the entity 
identified by Book(2) has already been persisted without a parent.  A 
parent cannot be established or changed once an object has been persisted.


What gives?  Is it not permitted to refer to objects in this way?  I've 
been using this technique in JPA for years.


Thanks,
Dave

--
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.



Re: [appengine-java] Object references imply ownership?

2010-10-04 Thread Dave Hicks

 Hi Hari,

I don't follow what you're saying in #1 below.

#2 - This is just the way I have always modeled classes using JPA.  In a 
relational database, the @ManyToOne would cause the key of the owning 
entity to be stored in the row.  In this case, the Rating table would 
have a column to hold the Person_Id.  It's unclear to me how this works 
with the DataNucleus datastore in GAE.  Here's an example of a similar 
relationship that is supposed to work in GAE:  
http://gae-java-persistence.blogspot.com/2009/10/creating-bidrectional-owned-one-to-many.html  
Obviously, the difference is that I have another @ManyToOne that 
references another entity type.


I've always felt like I had a firm handle on JPA, but using it with GAE 
has been a very discouraging endeavor, so far.

Thanks for the feedback,
Dave


On 10/04/2010 11:56 PM, Hariharan Anantharaman wrote:


Hi,
I have encountered similar error using JDO long time back. I firmly 
believe that if you use @ManyToOne or something it means a 
relationship amongst them.


Though I don't remember exactly what I did ,Coupleof observations
1. In the rating class. Add annotation of rederenced by attribute in 
annotation. Or a similar annotation needs to be done in Book class.


2. Person has a list of rating and rating again has a person. Are you 
planning to query by rating entity independently? I think this design 
tells JPA that a person indirectly owns a book.


Thanks
Hari

On Oct 5, 2010 2:54 AM, Dave Hicks dh...@i-hicks.org
mailto:dh...@i-hicks.org wrote:

 I'm using JPA, and I'm having a really tough time getting around
this problem.  It seems that when I try to refer to an object by
way of a @ManyToOne, somehow DataNucleus wants to assume some kind
of ownership and throws an exception.

I have three kinds of objects:  Person, Book, Rating.  Person and
Book objects are independent of one another and created
independently.  When a person rates a Book, a new Rating object is
created and added to a set of Ratings owned by the Person.  The
Rating object has a reference to the Book.  Here are the classes
(each extends a superclass that contains the @Id element):

class Person extends BasicEntity {
   private String email;

   @OneToMany(mappedBy=person, cascade=CascadeType.All,
fetch=FetchType.LAZY)
   private SetRating ratings;
}

class Book extends BasicEntity {
   private String name;
}

class Rating extends BasicEntity {
   @ManyToOne(fetch=FetchType.LAZY)
   private Person person;

   @ManyToOne(fetch=FetchType.LAZY)
   private Book book;

   private Integer value;
}

I get the following exception when I attempt to add such a Rating
to a Person:
org.springframework.orm.jpa.JpaSystemException: Detected attempt
to establish Person(1)/Rating(3) as the parent of Book(2) but the
entity identified by Book(2) has already been persisted without a
parent.  A parent cannot be established or changed once an object
has been persisted.

What gives?  Is it not permitted to refer to objects in this way?
 I've been using this technique in JPA for years.

Thanks,
Dave

-- 
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-java@googlegroups.com
mailto:google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to
google-appengine-java+unsubscr...@googlegroups.com
mailto: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.


--
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.



[appengine-java] Looking for a JPA/Entity-Group Tutorial

2010-10-03 Thread Dave Hicks
 I'm having problems understanding how to get entities to play nice 
together.  So, I'd like to find a good tutorial on how to work with 
entity groups using JPA.


My specific issue, right now, is being unable to add a new entity to a 
collection that is held by a parent entity.  When I attempt to add a new 
entity to the collection of children, I always get an error indicating 
that the entity group is incorrect.  I just haven't been able to find 
any information about how to make this work.


Thanks for any tips/links.
Dave

--
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.