Hi Andrea
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.
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.
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.
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 */
Hope this helps,
Sylvain Hamel
--- In SimpleORM@yahoogroups.com, "Andrea Broglia" <andreab@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