开发者

Recreating an entity using open JPA

开发者 https://www.devze.com 2023-03-27 03:53 出处:网络
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 per

I'm facing quite a basic issue (allegedly..). This is the structure I'm refering to:

Recreating an entity using open JPA

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

  1. participant
  2. advised
  3. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号