Andrea Broglia | 5 Sep 2003 04:01
Picon
Picon

RE: Re: Newbie questions

Hi Sylvain,

1) OK, I got it now. Originally I though that you were referring to a
"files" section in the Simpleorm.org web site. Now I've realised that it's
in the yahoo groups page :-)
BTW, there are some typos in the file: ".Equals" should be ".equals"

2) That's fine.

3) Reference:

I've tried this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class Product extends SRecordInstance {
  public static final SRecordMeta meta = 
    new SRecordMeta (Product.class, "Products"); 
  public static final SFieldInteger PROD_ID = 
    new SFieldInteger (meta,"prod_id",SFD_PRIMARY_KEY); 
  public static final SFieldString DESCRIPTION = 
    new SFieldString(meta,"description",255,SFD_MANDATORY);
  public static final SFieldReference HIST = 
    new SFieldReference (meta,PHistory.meta,"phist_",SFD_MANDATORY);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

But this would entail the presence of a "phist_id" column in my Products
table as FK, that I don't have.
Instead, in my table the "prod_id" column is both PK and FK.
Is there a way of having a reference where the PK equals the FK?

Another question about the SFieldReference constructor. What if there is no
relationships between the names of the referrer table's FK and the
referenced table's PK?
E.g. the FK in Table1 is "abc" and the corresponding column in Table2 is
"xyz"?
In this case you can't use this SFieldReference constructor because there's
no way of setting the "prefix" parameter.
I've noticed that there are other constructors for SFieldReference, like:
SFieldReference(SRecordMeta meta, SRecordMeta referenced, SFieldMeta[]
foreignKeys, SPropertyValue[] pvals).

But when I try to use in the Product class:

  public static final SFieldReference HIST = 
        new SFieldReference(meta, PHistory.meta, 
                            new SFieldMeta[] {PROD_ID}, 
                            new SPropertyValue[] {});

I keep getting the following exception:
  Inconsistent Primary Key flags [FR Product._PHistory] vs [F
Product.prod_id]

Any idea of the reason or what I'm doing wrong?

Cheers,
  Andrea

> -----Original Message-----
> From: sylvainhamel [mailto:sylvainhamel <at> hotmail.com] 
> Sent: Friday, 5 September 2003 12:00 AM
> To: SimpleORM <at> yahoogroups.com
> Subject: [SimpleORM] Re: Newbie questions
> 
> 
> Hi Andrea,
> 
> 1) For the updated driver look in the "Files" section. Look in the 
> group menu on the left of the screen.
> 
> 2) Forgot to mention it but should you decide to go with option a) 
> you would have to add your new driver in the list. You end up 
> modifying the core code but impact is minimal. Eventually you could 
> give Andrew the new driver so he can update the source code.
> 
> ----------------------
> static SDriver [] drivers = new SDriver[] {
> new SDriverHSQL(), new SDriverPostgres(), new SDriverMySQL(), 
> new SDriverOracle(), new SDriverMSSQL(), new SDriverAndrea(), new 
> SDriverDB2_400(), new SDriverInformix(), new SDriverInterbase()};
> -----------------------
> 
> 3) References : 
> >public static final SFieldReference ITEM =
> > new SFieldReference(meta, Product.meta, 
> > "prod_", SFD_MANDATORY);
> 
> This code wont work in the product because you end up adding a 
> reference toward another product (which is fine in itself if you 
> wanted to do that). The error you get is because of the 3rd param. 
> This param is a prefix for the relation field in the database. 
> Because of this you end up with 2 columns named "prod_id". You could 
> change it to "products_" or "_prod_" and it would work. Also I dont 
> think its wise to set a circular MANDATORY reference for obvious 
> reasons (I know you just recopied it).
> 
> Explaining the parameters of the SFieldReference consttructor will 
> make it simple : 
> 1) (meta) A refenrece to your record metada. In short the meta of the 
> object that you declared a few lines up.
> 2) (Product.meta) The metadata of the table you want to set a 
> reference on. This is where the error is. For it to work like you 
> want you should set it to PHistory.meta.
> 3) ("prod_") A prefix for the primary key fieds of the related table. 
> Why a prefix? Because the primary key of your master table might be 
> composite.
> 4) (SFD_MANDATORY) The properties of this field like mandatory (ie 
> NOT NULL), generated key, primary key, descriptive, ... Look into 
> SConstants for the full list.
> 
> So if I understand it correctly you want PHistory to be the master 
> record and Product to be its child? If so then you would need to 
> define it as follow : 
> 
> public class PHistory extends SRecordInstance {
> public static final SRecordMeta meta = new SRecordMeta 
> (PHistory.class,"PHistory"); public static final 
> SFieldInteger ID = new SFieldInteger 
> (meta,"id",SFD_PRIMARY_KEY,SFD_GENERATED_KEY);
> public static final SFieldString REASON = new SFieldString 
> (meta,"reason",255); }
> 
> public class Product extends SRecordInstance {
> public static final SRecordMeta meta = new SRecordMeta 
> (Product.class, "Products"); public static final 
> SFieldInteger PROD_ID = new SFieldInteger 
> (meta,"prod_id",SFD_PRIMARY_KEY); public static final 
> SFieldString DESCRIPTION = new SFieldString 
> (meta,"description",255,SFD_MANDATORY);
> public static final SFieldReference 
> (meta,PHistory.meta,"phist_",SFD_MANDATORY);
> }
> 
> This should do it though this isnt compiled and tested code so there 
> might be typos.
> 
> Hope it helps, 
> 
> Sylvain Hamel
> 
> --- In SimpleORM <at> yahoogroups.com, "Andrea Broglia" <andreab <at> d...> 
> wrote:
> > Hi Sylvain,
> >   first of all, thanks for your help.
> > 
> > > Before we start make sure you are using my SQLDriver has the one
> that 
> > > is included with simpleorm has some bugs in it. Go in the files
> > > sections for it. Replace the simpleorm one with this one.
> > 
> > Where exactly should I get the new file from?
> > 
> > > 1- How can I set a different jdbc driver for a database?
> > > 
> > > There are 2 ways of doing this.
> > > a) Add a new Driver which Overloads SDriverMSSQL and returns your
> > > identification string instead of weblogic. This might be the best 
> way 
> > > because different drivers for the same database often behave
> > > differently. It enables you to overload the methods with 
> different 
> > > internal behavior. Also you dont change the SimpleORM code so
> that if 
> > > there is a version update you dont overwrite your code as you
> would 
> > > if you modified the core simpleorm code.
> > 
> > I've tried this solution but it doesn't work, because the 
> > "SDriver.chooseDriver(...)" method iterates only through well known
> driver
> > classes.
> > ~~~~~~~~ snipped from simpleorm.core.SDriver ~~~~~~~~~~
> > ...
> > static SDriver [] drivers = new SDriver[] {
> >   new SDriverHSQL(), new SDriverPostgres(), new SDriverMySQL(), new 
> > SDriverOracle(),
> >   new SDriverMSSQL(), new SDriverDB2_400(), new SDriverInformix(),
> new
> > SDriverInterbase()};
> > ...
> > 
> >   static SDriver chooseDriver(java.sql.Connection con) {
> >     ...
> >     for (int dx=0; dx<drivers.length; dx++) {
> >       if (drivers[dx].driverName().equals(driverName))
> >         return drivers[dx];
> >     }
> >     ...
> >   }
> > 
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> > Thus, even if I add my own MySDriverMSSQL class, it doesn't get
> picked up by
> > SimpleORM, resulting into the following exception:
> > SException simpleorm.core.SException$JDBC: ???Executing 'SELECT 
> > consumer_identity_number, consumer_item_id, name_title, 
> family_name, 
> > given_name, other_given_names, name_suffix FROM
> PI_Registered_Consumer WHERE
> > consumer_identity_number = ?  FOR UPDATE' for [Consumer 35]:
> > java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for 
> > JDBC][SQLServer]FOR UPDATE cannot be specified on a READ ONLY
> cursor.
> > java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for 
> > JDBC][SQLServer]FOR UPDATE cannot be specified on a READ ONLY
> cursor.
> > 
> > 
> > 
> > > b) Specify driver upon SConnection.attach
> > > with  :
> > > attach(Connection con, String connectionName, SDriver driver) 
> > > This way simpleorm will ignore the metaname of the driver. That 
> way 
> > > you can even put the name of the driver you want in a
> configuration 
> > > file and load the driver at run time like your want.
> > 
> > This solution actually works, so I'll stick with this one. All the 
> > tests performed in the ant script fail, though.
> > 
> >  
> > > 2- I'm having problems in using SFieldReference for foreign keys.
> > > 
> > > Very easy to do :
> > > 
> > > public class Product extends SRecordInstance {
> > > public static final SRecordMeta meta = new SRecordMeta
> > > (Product.class, "Products"); public static final 
> > > SFieldInteger PROD_ID = new SFieldInteger 
> > > (meta,"prod_id",SFD_PRIMARY_KEY); public static final 
> > > SFieldString DESCRIPTION = new SFieldString 
> > > (meta,"description",255,SFD_MANDATORY);
> > > }
> > > 
> > > public class PHistory extends SRecordInstance {
> > > public static final SRecordMeta meta = new SRecordMeta
> > > (PHistory.class,"PHistory"); public static final 
> > > SFieldInteger ID = new SFieldInteger 
> > > (meta,"id",SFD_PRIMARY_KEY,SFD_GENERATED_KEY);
> > > public static final SFieldReference 
> > > (meta,Product.meta,"prod_",SFD_MANDATORY);
> > > public static final SFieldString REASON = new SFieldString 
> > > (meta,"reason",255); }
> > > 
> > > /* I added the SFD_GENERATED_KEY as an example though you didnt
> > > mention that your key was generated */
> > 
> > I actually wanted to do the other way round: having a reference in
> the class
> > Product to PHistory.
> > So I assume I should insert a line like the follwoing in the
> Product class:
> >   public static final SFieldReference ITEM = 
> >     new SFieldReference(meta, Product.meta, 
> >                         "prod_", SFD_MANDATORY);
> > 
> > I've tried this, but I got an exception:
> >   java.lang.ExceptionInInitializerError
> >   Caused by: simpleorm.core.SException$Error: ???Duplicate Column
> Name
> > prod_id
> > 
> > 
> > Any idea?
> > 
> > Thanks again,
> >   Andrea
> > 
> > 
> > 
> > 
> > > Hope this helps,
> > > 
> > > Sylvain Hamel
> > > 
> > > --- In SimpleORM <at> yahoogroups.com, "Andrea Broglia" <andreab <at> d...>
> > > wrote:
> > > > Hi,
> > > >   I'm a new user of SimpleORM, and I've been having few
> problems:
> > > > 
> > > > 1- How can I set a different jdbc driver for a database? In my
> case
> > > I was
> > > > using SQLServer as backend with the Microsoft jdbc driver
> > > > (com.microsoft.jdbc.sqlserver.SQLServerDriver).
> > > > To run the test (through the ant script), I've set my jdbc 
> driver
> > > in the
> > > > build.xml file using:
> > > > <property name="database.driver"
> > > > value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
> > > > Unluckily SimpleORM didn't pick that I was using SQLServer and 
> used
> > > the
> > > > generic SDriver class, rather than the specific SDriverMSSQL.
> This 
> > > > resulted into some SQLException because SQLServer doesn't
> > > support the
> > > > "FOR UPDATE" statement.
> > > > Looking at the source code I've noticed that the driver name is
> > > somehow hard
> > > > coded in the driver class (eg. SDriverMSSQL uses
> > > > "weblogic.jdbc.mssqlserver4.Driver").
> > > > I've then changed this value to "SQLServer" (that is the 
> > > driver name
> > > > returned by the Microsoft jdbc driver) in the SDriverMSSQL.java,
> > > recompiled
> > > > the lot and everything worked fine.
> > > > 
> > > > ~~~~~~~ Code snippet ~~~~~~~~ 
> > > >   ...
> > > >   public class SDriverMSSQL extends SDriver {
> > > >     protected String driverName() {
> > > >      //Old value: return "weblogic.jdbc.mssqlserver4.Driver";
> > > >      return "SQLServer";
> > > >     }
> > > >   ...
> > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > > 
> > > > I'm wondering if there is a more elegant and flexible way of
> doing
> > > this,
> > > > maybe through a configuration file.
> > > > 
> > > > 
> > > > 2- I'm having problems in using SFieldReference for foreign
> > > keys. This
> > > > is my scenario: I have a Products and a PHistory tables with
> > > the
> > > > following columns:
> > > >   Products:
> > > >     - prod_id (this is the PK of this table and FK for PHistory)
> > > >     - description
> > > >   PHistory:
> > > >     - id (this is the PK of this table)
> > > >     - replaced_by
> > > >     - reason
> > > > 
> > > > I created a class to map the Products table, like:
> > > > ~~~~~~ Code snippet ~~~~~~
> > > > public class Product extends SRecordInstance 
> > > >   implements Serializable {
> > > > 
> > > >   public static final SRecordMeta meta = 
> > > >     new SRecordMeta(Product.class, "Products");
> > > > 
> > > >   public static final SFieldInteger PROD_ID = 
> > > >     new SFieldInteger(meta, "prod_id", SFD_PRIMARY_KEY);
> > > >   
> > > >   ...
> > > > 
> > > >   public static void main(String[] args) {
> > > >     ...
> > > >     Product p = (Product)Product.meta.findOrCreate(new Integer
> (35));
> > > >     ...
> > > >   }
> > > > }
> > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > > 
> > > > Now, how can I extend this code and add a reference field to a
> > > PHistory
> > > > class?
> > > > If I add a line like:
> > > >   public static final SFieldReference HIST = 
> > > >     new SFieldReference(meta, PHistory.meta, 
> > > >                         new SFieldMeta[] {PROD_ID}, 
> > > >                         new SPropertyValue[] {});
> > > > 
> > > > I get an exception:
> > > >   java.lang.ExceptionInInitializerError
> > > >   Caused by: simpleorm.core.SException$Error: ???Inconsistent
> > > Primary Key
> > > > flags [FR Product._PHistory] vs [F Product.prod_id]
> > > > 
> > > > 
> > > > Thanks in advance for your help,
> > > >   Andrea
> > > 
> > > 
> > > ------------------------ Yahoo! Groups Sponsor
> > > ---------------------~--> Buy Ink Cartridges or Refill Kits 
> > > for Your HP, Epson, Canon or Lexmark Printer at Myinks.com. 
> > > Free s/h on orders $50 or more to the US & Canada. 
> > http://www.c1tracking.com/l.asp?cid=5511
> > http://us.click.yahoo.com/l.m7sD/LIdGAA/qnsNAA/5cFolB/TM
> > --------------------------------------------------------------------
> -~->
> > 
> > To unsubscribe from this group, send an email to: 
> > SimpleORM-unsubscribe <at> yahoogroups.com
> > 
> >  
> > 
> > Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
> 
> 
> ------------------------ Yahoo! Groups Sponsor 
> ---------------------~-->
> Buy Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark
> Printer at Myinks.com. Free s/h on orders $50 or more to the 
> US & Canada. http://www.c1tracking.com/l.asp?cid=5511
> http://us.click.yahoo.com/l.m7sD/LIdGAA/qnsNAA/5cFolB/TM
> --------------------------------------------------------------
> -------~->
> 
> To unsubscribe from this group, send an email to:
> SimpleORM-unsubscribe <at> yahoogroups.com
> 
>  
> 
> Your use of Yahoo! Groups is subject to 
> http://docs.yahoo.com/info/terms/ 
> 
> 

------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark
Printer at Myinks.com. Free s/h on orders $50 or more to the US & Canada. http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/l.m7sD/LIdGAA/qnsNAA/5cFolB/TM
---------------------------------------------------------------------~->

To unsubscribe from this group, send an email to:
SimpleORM-unsubscribe <at> yahoogroups.com

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 


Gmane