[Objectbridge-developers] AW: [Fwd: OJB Technical Questions & Tutorial #3]
Brought to you by:
thma
From: <tho...@it...> - 2001-05-22 12:41:25
|
Hi Sasha, > -------- Original Message -------- > Subject: OJB Technical Questions & Tutorial #3 > Date: Mon, 21 May 2001 16:09:47 -0400 > From: "Sasha Haghani" <ha...@in...> > To: <th...@us...> > > Dear Thomas, > > I've been taking a deeper look at OJB, including the examples found in > the > test package, and I have a few questions about features, mapping and > identities. > > I hope these don't bother you too much ;-), but I was hoping you could > answer these for me. No problem... > > TUTORIAL #3 > On the OJB Forum on SourceForge recently, you told a user > that you were > still working on a tutorial which "demonstrates basic OJB programming > techniques". I assume this is tutorial #3. Would you also be able to > send > me a copy of this -- it might help answer many of my questions. No, I meant tutorial 2, the ODMG tutorial, which is now finished and available under http://objectbridge.sourceforge.net/tutorial2.html. This tutorial expalins how to use the OJB ODMG API. The first tutorial http://objectbridge.sourceforge.net/tutorial1.html explains the OJB PersistenceBroker API. The third tutorial covering OJB O/R mapping techniques has not been started :-( > SUPPORT FOR MAPPING MANY-MANY RELATIONSHIPS > Does OJB currently support the mapping of many-to-many > relationships? I > didn't see any examples in the tests. If not, is this planned for an > upcoming release? > You are right, there is no support for m-n relationships. You have to decompose it into two 1-n relationships. You have to do this for your RDBMS ER model anyway, so its not such a big problem in my eyes. There are some OJB users (marquier et al.) who are thinking of building an n-m extension. But right now there are no concrete plans to include such a feature. > ONE-WAY 1-MANY RELATIONSHIPS > Is it always necessary for the many related object (i.e.: Article) to > have a > reference and an ID of the parent object (i.e.: > ProductGroup). In other > words, do the relationships always have to be two-way in the Java > objects? You always need the foreign key identifier(s), to build the proper SQL statements, but there is no need to have a reference attribute. So it's quite possible to define unidirectional associations in the java objects. > Is there a way to map such a relationship where ProductGroup > would only > need > to have a Collection or array of Articles (i.e.: one-way)? Yes. just omit the attribute productGroup from the class Article and remove it's reference descriptor from the repository xml file to see it. > > Is the <descriptor_ids> tag meant to reference the field descriptor > which > stores the parent's ID in a 1-N relationship? > Exactly. <ClassDescriptor id="2"> <class.name>test.ojb.broker.ProductGroup</class.name> <class.proxy>test.ojb.broker.ProductGroupProxy</class.proxy> <table.name>Kategorien</table.name> ... ... <CollectionDescriptor id="1"> <cdfield.name>allArticlesInGroup</cdfield.name> <items.class>test.ojb.broker.Article</items.class> <descriptor_ids>4</descriptor_ids> <auto.retrieve>true</auto.retrieve> <auto.update>false</auto.update> <auto.delete>false</auto.delete> </CollectionDescriptor> </ClassDescriptor> means that the fieldDescriptor with id 4 of the class test.ojb.broker.Article is used as a foreign key: <ClassDescriptor id="1"> <class.name>test.ojb.broker.Article</class.name> ... ... <FieldDescriptor id="4"> <field.name>productGroupId</field.name> <column.name>Kategorie_Nr</column.name> <jdbc_type>INT</jdbc_type> </FieldDescriptor> We need this this to build proper WHERE - clauses. If you have compound primary keys you can use a comma separated list to denote all foreign key descriptors (e.g. <descriptor_ids>4,5,6</descriptor_ids>) > Finally in the example repository.xml for the test package, in the > <CollectionDescriptor> which appears in the mapping for the class > test.ojb.broker.ProductGroup, the collection field name on line 109 > appears > as follows: > > <cdfield.name>allArticlesInGroup</cdfield.name> > > But there is no getAllArticlesInGroup() method in ProductGroup, only > getAllArticles(), and the allArticlesInGroup member Vector is declared > as > private. So how does OJB read/write this field/property? > As you see there is no need to stick to naming conventions as in EJB. OJB does not use getters and setters to access the attributes of persistent classes. It does access exactly those attributes that are defined in the repository xml file. It does use the Java reflection API to do this, which allows to access private attributes. > ASSIGNING IDENTITY VALUES > Some of the object graphs that I will be persisting have > dependent/related > objects which do not have IDs. Do all objects have to have IDs when > persisting using OJB? If so, is there a way to have OJB transparently > assign IDs using the SequenceManager, without the client code > needing to > call SequenceManager.getUniqueId() for each object? If not, > can I use a > ConversionStrategy as a means of implementing transparent ID > assignment? I'm not sure if I get you right: Your database table have no primary key columns? Or do they have non numeric keys? Or do you have compound keys, that contain of two or more columns? OJB does require to have primary keys. But they can be of arbitrary type (not only INT) and may also be compound keys. The SequenceManager can be used to generate unique primary key values for columns of type int. There is no repository tag like <autoincrement> that triggers automatical generation of IDs, thus you have to place the SequenceManager.getUniqueId() in your client code. But if you need this feature urgently just tell me. It can be easily included in the next release. You can't use ConversionSTrategies on primarykey columns. (For performnce reasons, etc.) > > OJB TABLES AND INTERNAL MAPPINGS > Are the additional OJB tables and internal mapping always required, or > are > they only needed when using ODMG collection support? > You are right: those internal tables are only needed if you use special features. There is one table that contains the values for the SequenceManager. All other tables are used by the ODMG server. See this discussion for a documentation of the internal tables: http://sourceforge.net/forum/forum.php?thread_id=97263&forum_id=43066 HTH, Thomas |