Primary keys? Unique constraint?
"Until they become conscious they will never rebel, and until after they have rebelled they cannot become conscious" --Orwell 1984 ----- Original Message ----- From: "Shane Mingins" <[EMAIL PROTECTED]> To: "'OJB Users List'" <[EMAIL PROTECTED]> Sent: Thursday, June 12, 2003 11:52 PM Subject: RE: ODMG API: 1->n [My Example] > Aggh! This only works the first time through. Subsequent attempts to add > do not :-( > > If anyone spots why, let me know, otherwise will look on Monday. > > Cheers > Shane > > -----Original Message----- > From: Shane Mingins [mailto:[EMAIL PROTECTED] > Sent: Friday, 13 June 2003 12:49 p.m. > To: '[EMAIL PROTECTED]' > Subject: ODMG API: 1->n [My Example] > > Hi All > > Having struggled with using a collection to hold a 1->n relationship using > the ODMG API I have come up with the following basic code (to be refactored > later). > > I would appreciate any comments (especially as I would not want others to > use it if I have made errors). > > Below the code is the database mappings, classes etc for absolute > transparency. > > Cheers > Shane > > public static void main(String[] args) > { > Implementation odmg = OJB.getInstance(); > Database db = odmg.newDatabase(); > try > { > db.open("default", Database.OPEN_READ_WRITE); > } > catch (ODMGException e) > { > e.printStackTrace(); > } > > // Create a new Distribution Channel with two products > DistributionChannel dc = new DistributionChannel("New > Channel","Swiss Bank A/c"); > dc.addProduct(new Product("P1","Product One")); > dc.addProduct(new Product("P2","Product Two")); > > Transaction tx = odmg.newTransaction(); > tx.begin(); > db.makePersistent(dc); > tx.commit(); > > > // Update channel and products > tx.begin(); > OQLQuery query = odmg.newOQLQuery(); > String oql = "select all from " + > DistributionChannel.class.getName() + " where name = $1"; > DList results = null; > try > { > query.create(oql); > query.bind("New Channel"); > results = (DList)query.execute(); > } > catch (QueryException e) > { > e.printStackTrace(); > } > DistributionChannel channel = (DistributionChannel)results.get(0); > > // Changing attributes to the Distribution Channel or existing > products is okay > channel.setCmaBankAccount("Name Changed"); > channel.getProduct("P2").setDescription("Desc Changed"); > > // Add a new product to the collection and persist it to the > database > Product pNew = new Product("P3","Product Three Added"); > channel.addProduct(pNew); > db.makePersistent(pNew); > > // Delete a product from the collection and database > Product pDel = channel.getProduct("P1"); > channel.removeProduct(pDel); > db.deletePersistent(pDel); > > // Persist changes to Distribution Channel to database > db.makePersistent(channel); > tx.commit(); > } > > <<-- BELOW IS THE SUPPORTING INFORMATION ->> > > The model is like this: > > --------------------------- --------------- > | DistributionChannel | | Product | > --------------------------- --------------- > | name | <---------------- | name | > | cmabankAccount | | description | > | products | | | > --------------------------- --------------- > > The mappings are like such: > > <class-descriptor class="com.assureinternational.ims.model.Product" > table="product"> > <field-descriptor name="name" column="name" jdbc-type="VARCHAR" > primarykey="true" /> > <field-descriptor name="distributionChannelName" column="distchannelname" > jdbc-type="VARCHAR" primarykey="true" /> > <field-descriptor name="description" column="description" > jdbc-type="VARCHAR"/> > </class-descriptor> > > <class-descriptor > class="com.assureinternational.ims.model.DistributionChannel" > table="distributionchannel"> > <field-descriptor name="name" column="name" jdbc-type="VARCHAR" > primarykey="true"/> > <field-descriptor name="cmaBankAccount" column="cmabankaccount" > jdbc-type="VARCHAR"/> > <collection-descriptor name="products" > element-class-ref="com.assureinternational.ims.model.Product" > > <inverse-foreignkey field-ref="distributionChannelName"/> > </collection-descriptor> > </class-descriptor> > > The classes are like this: > > package com.assureinternational.ims.model; > > import java.util.Collection; > import java.util.Iterator; > import java.util.Vector; > > public class DistributionChannel > { > private String name; > private String cmaBankAccount; > private Collection products = new Vector(); > > public DistributionChannel() > { > } > > public DistributionChannel(String name, String cmaBankAccount) > { > this.name = name; > this.cmaBankAccount = cmaBankAccount; > } > > public String getName() > { > return name; > } > > public String getCmaBankAccount() > { > return cmaBankAccount; > } > > public void setCmaBankAccount(String cmaBankAccount) > { > this.cmaBankAccount = cmaBankAccount; > } > > public void addProduct(Product product) > { > this.products.add(product); > } > > public Product getProduct(String name) > { > for (Iterator iterator = products.iterator(); iterator.hasNext();) > { > Product product = (Product) iterator.next(); > if (product.getName().equals(name)) > return product; > } > return null; > } > > public void removeProduct(Product product) > { > this.products.remove(product); > } > } > > > package com.assureinternational.ims.model; > > public class Product > { > private String distributionChannelName; // OJB > private String name; > private String description; > > public Product() > { > } > > public Product(String name, String description) > { > this.name = name; > this.description = description; > } > > public String getName() > { > return name; > } > > public String getDescription() > { > return description; > } > > public void setDescription(String description) > { > this.description = description; > } > } > > > > Shane Mingins > Analyst Programmer > Assure NZ Ltd > Ph 644 494 2522 > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
