From: Jon M. <jo...@te...> - 2006-05-23 14:54:53
|
You're getting confused about primary and secondary keys. A primary key is the field in the table that uniquely identifies each record in that table. A secondary key is a field that refers to a record in a different table, i.e. to that other table's primary key. The Java type PrimaryKey is used to embody the key for any table in Bodington. Since the data type for secondary keys must be identical to the data type for primary keys the same Java class is used for both. (In other words, a secondary key is a copy of a primary key from another table and so it's data type is also 'PrimaryKey' although it is being used as a secondary key.) [By the way, I haven't made all this up - it's pretty standard database terminology] The standard finder method for any PersistentObject looks for an object based on the primary key database field. Since you provided the primary key for a User object, then a User object is what you got. A User object is not a UserDetail object so obviously you got a casting error. If you want to find a UserDetail object by its primary key then you need to pass the primary key of the UserDetail object - i.e. a PrimaryKey instance that refers to the value in the field user_detail_id. However, what you wanted to do was a *secondary* key lookup on the table user_details using a primary key from the table users. There are two options - use the finder method that takes an SQL WHERE clause or add a new finder method findUserDetailByUserId( PrimaryKey user_id ); The latter protects you from accidentally producing silly WHERE clauses which throw exceptions at run time and would contain a single line of code which I provide below. public static UserDetail findUserDetailByUserId( PrimaryKey user_id ) throws BuildingServerException { return (UserDetail)findPersistentObject( "user_id IS " + user_id, "org.bodington.server.realm.UserDetail" ); } In most databases a single integer value for a primary key could be used in all the tables. I.e. the full unique identifier for a record must use a combination of the table name and the primary key. So you can't expect the database to work out which field to match the key you provided against. The SQL database can't know that the key you provided should be matched against a secondary key if it failed to find a match against the primary key. It's the old adage: computers don't do what you want them to do, only what you tell them to do. Jon P.S. I was let out of court early today, hence this Email. Alistair Young wrote: > That's really weird. So the only way you can get a UserDetail from a > PrimaryKey is if you have the PrimaryKey of the UserDetail which you > got from the UserDetail? and PrimaryKey can mean anything! It's a > VARIANT in COM speak. Roll on Tetra! > > Alistair > > > On 22 May 2006, at 21:22, Matthew Buckett wrote: > >> Alistair Young wrote: >>> Before I dive off into bodguts, has anyone seen this before, or any >>> suggestions as to why it happens? >>> UserDetail bodUserDetail = >>> UserDetail.findUserDetail(user.getPrimaryKey()) >>> where user = org.bodington.server.realm.User >> >> Yep this is because UserDetail has it's own primary key and the >> primary key of the refering User. Your asking for the UserDetail >> object with the primary key of the User which doesn't exist. >> >>> always gives a ClassCastException. Is a User.PrimaryKey different >>> from a UserDetail.PrimaryKey? >> >> You get this because the database layer is trying to case a User >> object found by its primary key into a UserDetail object. >> >>> Always have to use sql to get UserDetail from a User. >> >> Yep. UserDetail.findUserDetail("user_id = ?"); Or make a >> findUserDetailByUser which does the SQL behind the scenes. You may >> also want to make user_id an indexkey if performance becomes a problem. >> >> -- -- Matthew Buckett, VLE Developer >> -- Learning Technologies Group, Oxford University Computing Services >> -- Tel: +44 (0)1865 283660 http://www.oucs.ox.ac.uk/ltg/ >> >> >> ------------------------------------------------------- >> Using Tomcat but need to do more? Need to support web services, >> security? >> Get stuff done quickly with pre-integrated technology to make your >> job easier >> Download IBM WebSphere Application Server v.1.0.1 based on Apache >> Geronimo >> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 >> _______________________________________________ >> Bodington-developers mailing list >> Bod...@li... >> https://lists.sourceforge.net/lists/listinfo/bodington-developers > > > > ------------------------------------------------------- > Using Tomcat but need to do more? Need to support web services, security? > Get stuff done quickly with pre-integrated technology to make your job > easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache > Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Bodington-developers mailing list > Bod...@li... > https://lists.sourceforge.net/lists/listinfo/bodington-developers > |