JR Galia
JR Galia

Reputation: 17269

One To One Relationship in JPA (AppEngine)

In my Profile class I have

@OneToOne(cascade=CascadeType.PERSIST)
private ProfilePicture profilePic = null;

My method in updating the profilePic

public Profile updateUserProfilePic(Profile user) {
    EntityManager em = EMF.get().createEntityManager();
    em.getTransaction().begin();

    Profile userx = em.find(Profile.class, user.getEmailAddress());
    userx.setProfilePic( user.getProfilePic() );

    em.getTransaction().commit();
    em.close();
    return userx;
}

When updateUserProfilePic is called, it just add another profilePic in datastore, it doesn't replaced the existing profilePic. Is my implementation correct? I want to update the profilePic of profile.

Upvotes: 0

Views: 346

Answers (1)

DataNucleus
DataNucleus

Reputation: 15577

"Transient" means not persistent and not detached.

Using that version of GAE JPA you need a detached or managed object there if you want it to reuse the existing object.

Using v2 of Googles plugin there is a persistence property that allows merge of a transient object that has "id" fields set.

Upvotes: 1

Related Questions