2

When I'm trying to commit Transaction I'm getting:

javax.persistence.RollbackException: Transaction failed to commit
javax.persistence.PersistenceException: Object with id "" is managed by a different 
org.datanucleus.exceptions.NucleusUserException: Object with id "" is managed by a different Object ManagerObject Manager

My Code is:

@Entity
public class Profile implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id     
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long profileId;

    private String m_Name;    
    private String m_Email;
    private String m_Password;  

    @ManyToOne()
    private List<MyPoint> m_points = null;
    .
    .
}

@Entity
public class MyPoint  implements Serializable 
{
    private static final long serialVersionUID = 1L;    
    @Id
    private int pointId;

    private int m_LatitudeE6;
    private int m_LongitudeE6;
    .
    .
}
  • If I'm deleting m_points with his annotations everything works fine.
  • I'm using JPA 1.0 with Google App Engine
    What am I doing wrong?
    Thanks.
Rami
  • 2,098
  • 5
  • 25
  • 37
  • 1
    See http://stackoverflow.com/questions/1403515/appengine-datastore-object-with-id-is-managed-by-a-different-object-manage. – cklab Jun 19 '12 at 23:42
  • 1
    I saw this question. Although that we have the same exeception it is not relevant because he is using JDO and not JPA. – Rami Jun 20 '12 at 06:03
  • You're using v1 of Google's plugin, that counts as something you're doing wrong. Use v2 since it fixes many problems with their first release. – DataNucleus Jun 20 '12 at 06:46
  • 1
    The other user is using JDO yes, and you're using JPA, and they have the exact same persistence engine underneath them, and the exact same answer applies here. Your persistence code is causing some object to be attempted to be used with 2 different EntityManagers – DataNucleus Jun 20 '12 at 07:21
  • According to GAE JPA 2.0 is experimental. Although You recommend using it? – Rami Jun 20 '12 at 09:32
  • "Experimental" is google-speak for "hasn't been around for at least 5 years" I think. It passes way more tests than their v1 plugin does (and doesn't fail any that theirs passes), so is stable by design. If you have no existing data then it is definitely recommended – DataNucleus Jun 20 '12 at 09:46

1 Answers1

1

Not a GAE expert, but there are some obvious things wrong with your entity mappings that need to be fixed.

First, it makes no sense to apply a @ManyToOne to a list (think about it, your are indicating, when you use that annotation, that many of this entity relate to one of the mapped entity).

Second, you have not specified the actual mapping columns for the embedded collection of points. You must either define the mapping from point to profile on the point class, or use a join table. Below is an example (very rough pseudo-code):

@Entity
public class Profile implements Serializable { 

    @OneToMany(mappedBy = "profile")
    private List<MyPoint> m_points = null;
}

@Entity
public class MyPoint implements Serializable {

    @ManyToOne
    @JoinColumn(name = "profileId")
    private Profile profile;
}

Quite obviously, the above example presumes that there is a foreign key named profileId on the MyPoint table that references the primary key on the Profile table.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • It seems strange that I need to keep reference to profile. I just want to hold list of points. – Rami Jun 20 '12 at 09:39
  • You don't *need* to hold a reference to profile in your point class (I outlined the situation where you would), you can use a join table to define the relationship, or now that I think of it and even better, an @EmbeddedCollection) – Perception Jun 20 '12 at 11:55