Recreating an entity using open JPA
I'm facing quite a basic issue (allegedly..). This is the structure I'm refering to:
I'm working with flex in the client side, Java & open JPA in the server side and microsoft SQL server persistency layer.
In my app (in the client side) it is possible to decide if a client is
- participant
- advised
- not advised.
Suppose I have 'Joe' as an advised client and now I'm converting Joe to 'not advised' (this is done from the client side by simply un-checking a check nox).
In the backend, I'm removing the current entity of Joe (as advised client), setting it's id to null and creating it again as 'not advised client'. Something like tha开发者_StackOverflowt:
getAdviseeDao().delete(currentPersistedAdvisee);
currentAdvisee.setId(null);
getAdviseeDao().create(currentAdvisee);
I keep on getting
com.microsoft.sqlserver.jdbc.SQLServerException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_advised_to_clients". The conflict occurred in database "blablabla", table "dbo.participants", column 'id'.
Any ideas why this may happen?
getAdviseeDao().delete(currentPersistedAdvisee);
currentAdvisee.setId(null);
getAdviseeDao().create(currentAdvisee);
JPA just doesn't work that way. Don't set IDs to null and don't try to re-persist an entity as something else. You'll get weird and unexpected behavior.
T think you should go with Aggregation instead of Inheritance. I guess I would encapsulate the advised part in an Embeddable class.
精彩评论