Re: [OJB-developers] Field descriptor order requirement and primary keys
Brought to you by:
thma
From: Thomas M. <tho...@ho...> - 2002-04-03 10:13:12
|
Hi John, John Freeborg wrote: > I guess I'm confused. > > In my existing database schema, the primary key column(s) are not always > the first ones in the table. No problem. > > Also, I sometimes have "extra" columns in a table that I don't want OJB > to do anything with so they aren't even in the repository.xml as it > would just slow things down more. No problem. > Is this allowed as long as those > columns have default values to handle any sql inserts OJB would perform? > OJB won't touch these columns, as it does not know anything about them! > With that background, what should I be setting my id="" attributes of > the FieldDescriptor's to? They can't possibly be the order of the > columns in the table because then the one with > <PrimaryKey>true</PrimaryKey> will not have id="1" as it is the 4th > column in the table. > Say we have this table: CREATE TABLE PRODUCT ( NAME CHAR(100), PRICE DOUBLE, ID INT PRIMARY KEY, HIDDEN VARCHAR(1000), STOCK INT ) This is the persistent class: public class Product { private int id; protected String name; protected double price; // column HIDDEN is not mapped to any attribute protected int stock; } This is the ClassDescriptor: <ClassDescriptor id="product"> <class.name>tutorial.Product</class.name> <table.name>PRODUCT</table.name> <FieldDescriptor id="1"> <field.name>name</field.name> <column.name>NAME</column.name> <jdbc_type>CHAR</jdbc_type> </FieldDescriptor> <FieldDescriptor id="2"> <field.name>price</field.name> <column.name>PRICE</column.name> <jdbc_type>DOUBLE</jdbc_type> </FieldDescriptor> <FieldDescriptor id="3"> <field.name>id</field.name> <column.name>ID</column.name> <jdbc_type>INT</jdbc_type> <PrimaryKey>true</PrimaryKey> <autoincrement>true</autoincrement> </FieldDescriptor> <FieldDescriptor id="4"> <field.name>stock</field.name> <column.name>STOCK</column.name> <jdbc_type>INT</jdbc_type> </FieldDescriptor> </ClassDescriptor> The ids of the FieldDescriptors correspond directly to the *order* of the mapped table columns. E.g. the stock column is *not* the fourth column of the table. but it is the fourth *mapped* column. >>From the RsIterator code snippet below, there is an assumption that the > primary keys are always first in the m_row member. this would be an uneccessary restriction! > And the ordering of > the m_row member seems to correspond to the id="" numbers from the field > descriptor. That's right > > So, my assertion is that if you have a field descriptor with > <PrimaryKey>true</PrimaryKey> and it is not id="1" then OJB doesn't > work. As long as you maintain the order of the mapped columns in the FK ids OJB does work! > And by extension if you had a compound primary key with multiple > field descriptors having <PrimaryKey> elements, they would need to be 1, > 2, 3, etc. they might as well be 4, 17, 23 ! > Is that just the way it is designed? I don't see this documented > anywhere, but I may have missed it of course. > I thought it was documented somewhere, but did not find it :-( > If this is true, I'll volunteer to add some checking of this during the > parsing of repository.xml as I think this is a pretty error prone part > of mapping. > Maybe it would be the best thing to eliminate the id based lookup of columns as there are several things like freeform SQL queries that don't work well with this approach. cu, Thomas > Thanks, > - John > > > -----Original Message----- > From: Thomas Mahler [mailto:tho...@ho...] > Sent: Tuesday, April 02, 2002 2:58 PM > To: John Freeborg > Cc: Obj...@li... > Subject: Re: [OJB-developers] Field descriptor order requirement and > primary keys > > Hi John, > > FieldDescriptor need a numeric ID. The Ids must ordered ascending > according to the corresponding columns in the database table. > > The physical position in the xml repository is not important. > > HTH, > > Thomas > > There is some code that > > John Freeborg wrote: > > >>Does OJB have a requirement that in the repository.xml file, all >> > primary > >>key field descriptors must be first inside a class descriptor? >> >>I don't have it that way (my primary key field descriptor happened to >> > be > >>number 4, not 1). After scratching my head a lot on why my query for >> > a > >>collection looked so strange, I hauled out BugSeeker and found the >>following code marked with ---> below that seems to assume a certain >>primary key field descriptor order. Interestingly the line above that >>is commented out doesn't appear to have this problem. >> >>Is this an unintended side effect of the optimization? Is this >> > broken? > >>Thanks, >> - John >> >> >>>From RsIterator.java in 0.8.361: >> >> protected Identity getIdentityFromResultSet() throws >>PersistenceBrokerException >> { >> try >> { >> // fill primary key values from Resultset >> FieldDescriptor fld; >> FieldDescriptor[] pkFields = m_mif.getPkFields(); >> Object[] pkValues = new Object[pkFields.length]; >> >> for (int i = 0; i < pkFields.length; i++) >> { >> fld = pkFields[i]; >> //pkValues[i] = JdbcAccess.getObjectFromColumn(m_rs, >>fld); >>---> pkValues[i] = m_row[i]; >> } >> // return identity object build up from primary keys >> return new Identity(getExtentClass(), pkValues); >> } >> catch (Throwable t) >> { >> logger.error(t); >> throw new PersistenceBrokerException(t); >> } >> } >> >>_______________________________________________ >>Objectbridge-developers mailing list >>Obj...@li... >>https://lists.sourceforge.net/lists/listinfo/objectbridge-developers >> >> > > _______________________________________________ > Objectbridge-developers mailing list > Obj...@li... > https://lists.sourceforge.net/lists/listinfo/objectbridge-developers > > > > |