A simple example or a simple yet concret tutorial fo simpleORM !!!
abdessamad ajnan <[email protected]> Sat, 27 Dec 2008 03:31:17 -0800 (PST)
| Newsgroups | gmane.comp.java.orm.simpleorm |
|---|---|
| Message-ID | <[email protected]> |
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.