• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Entity Relationship: duplicating record with GWT/Google App Engine

 
Dominik Müller
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi together,

I am working on a little GWT Project on GoogleAppEngine. Now i have a little problem with the persistence of an entity, maybe with a general JPA Problem, don't know.

***THE CODE***
The "Root"-Entity looks like this


with a relationship to:
(the Key-Class is an GoogleAppEngine object)



*** THE PROBLEM ***
Because of the "Key-Class", which isn't present for the client (without no really hooks) i have to implement a convertion between the Entities (Server) and a simple DTO(Client). A simle general communications looks like this

[Client] Creates an CodingDTO and a CategoryDTO (with an long as indentifier) to persists
[Client] Sends CodingDTO to Server
[Server] Convert CodingDTO to Coding
(the Key should re-generated with the long-ID, but here i implement even a Reload
[Server] with the CategoryID from the submittet from the Client, i reload the existing Category from the Database
[Server] adds the Category to the Coding and persists the Coding on DB

here the loading the category from the DB, which added to the coding to store:


*** THE RESULT ***

The coding is stored correct.
But the related "category" which is added to the coding is stored in the database too, although this category exist already, and duplicate while "persists" the object, and generate a new id

No Ideas any more. I guess only about the Cascace-Types, but only the "ALL" is working only, other arguments are crashed. Maybe it is a GWT/Google App Engine error, but maybe a JPA too. Hope some have some good ideas.

Thanks a lot
Dom

PS: Topic should better moved to "Object Relational Mapping" of course, sorry
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dominik Müller wrote:PS: Topic should better moved to "Object Relational Mapping" of course, sorry

I agree. Moving for you.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please include some details on how you persist the Coding object.

If it has an existing Category, you should read that category in the same transaction before setting it on the Coding.
Or, calling merge() instead of persist() my work.
 
Dominik Müller
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sutherland wrote:Please include some details on how you persist the Coding object.
If it has an existing Category, you should read that category in the same transaction before setting it on the Coding.


Hi,

here the full Method to save my Coding Object, with some comments



But the Result is still that not only the new Coding is written to the database, the existing Category (with an new auto generated ID) too

Thanks again
Dominik
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see how that could occur, as you are reading the Category through the EntityManager in the same transaction.

Check the Id of the Category after you assign it to the coding, maybe something in your code it setting it to null.

What JPA provider are you using? Can you include the logging trace and your persistence.xml?
 
Dominik Müller
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for helping so far.

Got the Problem i thing, although the result isn't so fine as i exptected.

First of all the Relationship definition was not really correct:




Here i have the correct One To Many Relationsship. The Problem here is that the Google-Implementation seems to restricted that kind of the Relationsship.
When i use a real Category-Object in the Coding-Object, Googles talks about an owned-relationsship, which will be stored in the key of the category. That means that this stored category never can be used in another coding object.
What i really need is a so called, unowned-Relationsship, which means that all category objects could use in different CodingEntities. the only oppurtunity, as far as i know, is to replace the composition of the objects with an identifier instead, works with:



a simple foreign key. Not really a nice opportunity i thing, but so far the only one i found :/

Dom
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic