Using JPA CompositeKey uses a property from another Entity -
i tried searching having problems finding want. have following schema( * indicates primary key)
user *userid -desc registration *deviceid *userid -date so want create primary key registration like...
@embeddable public class regpk{ private string deviceid; private user user; @column(name="dev_id") public string getdevid(){ return deviceid; } ... @manytoone @joincolumn(name="user_id", referencedcolumnname="user_id") public user getuser() { return user; } ... } is right? update user_id field update in registration when persist?
when try kind of thing out following....
[10/7/13 13:37:07:156 edt] 000000ae webapp e com.ibm.ws.webcontainer.webapp.webapp logservleterror srve0293e: [servlet error]-[hello]: org.apache.openjpa.util.metadataexception: id class specified type "class org.me.mfa.jpa.registration" not match primary key fields of class. make sure identity class has same primary keys persistent type, including pk field types. mismatched property: "user"
so now?
jpa not allow primary key classes contain relationships, basic types. jpa 2.0 allows relationships apart of id, move relationship entity class, , have regpk contain deviceid , userid. device-user relationship marked either @id or @mapsid("user") depending on if wanted use @pkclass or @embeddedid within entity. see http://wiki.eclipse.org/eclipselink/examples/jpa/2.0/derivedidentifiers example using pk class.
in jpa 1.0, use similar setup, except device-user relationship marked read-only (either specifying @primarykeyjoincolumn annotation, or marking @joincolumn insertable=false, updatable=false). need set primary key basic mapping value manually, pulling value referenced user entity directly. of course requires id in user assigned, might require additional work if both objects new.
Comments
Post a Comment