Re: A simple example or a simple yet concret tutorial fo simpleORM !!!

Anthony Berglas <[email protected]> Mon, 12 Jan 2009 10:22:36 +1000
Newsgroups gmane.comp.java.orm.simpleorm
Message-ID <[email protected]>
Hello,

Your code looks generally OK.   But there is a lot to read through without some diagnostics.

When you say it "does not work" what does it actually do?  Does it throw an exception?  What output do you get?  (Traces are on by default.)

The following line in createTable_Vin is unnecessary and a little ugly -- you are using statics to define the meta schema. (There are other ways to structure this, but statics are usually the most convenient.)
       Vin v=new Vin();         

If you want data to persist between invocations in HSQL you will need to use url "jdbc:hsqldb:hsqlTempFiles;shutdown=true;".

Sorry for the delay but we have been away.

Anthony


At 09:31 PM 27/12/2008, you wrote:

>Hi,
>I am a newbie with simpleORM. the examples provided with the distribution and the white paper on the site (simpleorm.org) are too complex for me. I can't do concret things such as set up a database and create some tables on it and write/read data from them.
>I wonder if there is a step-by-step tutorial for simpleORM?
>Any way, i have a small project and these are the files :
>remember all i want to do is create tables on a HSQL DB, write data to it and read data from it with a ORM (that hide JDBC to me).
>file 1: "MyDataSource.java" creates a data source object that represents the database and offers a method that return a connection object to the real database.
>
>public class MyDataSource implements DataSource {
>    private String dburl="jdbc:hsqldb:hsqlTempFiles";
>    private String dbUserName="sa";
>    private String dbPassword="";
>    private String dbDriver="org.hsqldb.jdbcDriver";                 
>    
>    public MyDataSource() {                      
>        try {
>            Class.forName(dbDriver);
>            System.out.println("MyDataSource::MyDataSource le driver <"+dbDriver+"> charge correctement");
>        } catch (Exception ex) {
>            throw new SException.Jdbc("Loading " + dbDriver, ex);
>        }
>    }
>    public Connection getConnection() throws SQLException {
>        Connection con = java.sql.DriverManager.getConnection(dburl, dbUserName, dbPassword);
>        return con;
>    }
>........
>}
>
>
>file 2 : "Utilitary.java", like the TestUte of the distri this class is indeed to initialize the background of the test.
>
>public class Utilitary { // like TestUte         
>     
>    public SSessionJdbc initializeTest(String testName, boolean deprecated) {         
>       return SSessionJdbc.open(makeDataSource(), testName); 
>    } 
>     
>    private DataSource makeDataSource() { 
>        //String dataSourceName= systemProperties.getProperty("database.datasource",  null); 
>        //String dataSourceName="maDataSource"; 
>        DataSource ds; 
>        ds= new MyDataSource();  
>         
>        return ds; 
>    }
>....
>}
>
>file 3 : "Vin.java", a class that represents my table "Vin" (wine in english)
>
>public class Vin extends SRecordInstance {      
>    public static final SRecordMeta<Vin> VIN  
>        = new SRecordMeta<Vin>(Vin.class,    "TABLE_Vin"); 
> 
>    // ie. SRecord objects describe SRecordInstances   
>    public static final SFieldInteger id_v = new SFieldInteger(VIN, "ID_V", SPRIMARY_KEY);         
>     
>    public static final SFieldString nom_v = new SFieldString(VIN, "nom_v",12); 
>    public static final SFieldString cru_v = new SFieldString(VIN, "cru_v",12); 
>    public static final SFieldString region_v = new SFieldString(VIN, "region_v",12); 
>       public @Override() SRecordMeta<Vin> getMeta() { 
>        return VIN; 
>    };    
>}
>
>
>file 4 : Program.java , the main program and will use the other classes (defined in the 3 previous files)
>
>public class  Program {  
>     
>    public static void main(String[] argv) throws Exception {          
>        Utilitary util= new Utilitary(); 
>        SSessionJdbc ses; 
>        ses=util.initializeTest("Program", true); // use deprecated attach 
>        try { 
>            creerTable_Vin(ses); // cree la table VIN 
>            writeTable_Vin(); 
>            readTable_Vin();             
>        } finally { 
>            SSessionJdbc.getThreadLocalSession().close(); 
>        } 
>    } 
> 
>    /** Prepare for tests, Delete old data. */ 
>    static void createTable_Vin(SSessionJdbc ses) throws Exception {         
>        //SSessionJdbc ses = SSessionJdbc.getThreadLocalSession();         
>        ses.begin();                 
>        ses.flush(); 
>        Vin v=new Vin();         
>        //ses.rawUpdateDB(ses.getDriver().createTableSQL(Vin.VIN)); 
>        ses.flush(); 
>        ses.commit();         
>    } 
> 
>    static void readTable_Vin(){ 
>        int key=1; // juste comme �a 
>        SSessionJdbc ses = SSessionJdbc.getThreadLocalSession(); 
>        ses.begin(); 
>        Vin vin_cl = ses.findOrCreate(Vin.VIN, key); 
>        vin_cl.assertNotNewRow();             
>        System.out.println("vin_cl :"+vin_cl.allFields().toString());         
>    } 
>     
>    static void writeTable_Vin(){ 
>        SSessionJdbc ses = SSessionJdbc.getThreadLocalSession(); 
>        ses.begin();         
>        SDataLoader vin_DL = new SDataLoader(ses, Vin.VIN); // charge VIN dans vin_DL 
>        Vin v = (Vin) vin_DL.insertRecord("100","vin_blanc","cru_blanc","region_blanc");         
>        ses.flush(); // SQL Insert         
>        ses.commit(); 
>    } 
>}
>
>
>If you have a look in the "createTable_Vin", "readTable_Vin", and "writeTable_Vin" methods, can you tell me if something is missing?
>because the code given here such (it is), it does nothing i mean it doesn't create a table (TABLE_Vin) and it doesn't write any records into it.
>Can you give me a usefull tutorial address for simpleORM?
>
>SOS.
>
>
> 

Dr Anthony Berglas, [email protected]       Mobile: +61 4 4838 8874
Just because it is possible to push twigs along the ground with ones nose
does not necessarily mean that is the best way to collect firewood.

------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/SimpleORM/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/SimpleORM/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:[email protected] 
    mailto:[email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
556e0638.jpg (image/jpeg, 3.3 KB) - not displayed