Hi,
I am new with Hibernate, trying to access an Oracle 9 database server. All I did was create a set of files located at "hibernate-2.1\eg\org\hibernate\demo". The files are as follows:
Stuffed.java
Code:
package org.hibernate.demo;
public class Stuffed {
private String Data;
public Stuffed () {
}
public Stuffed (String a) {
this.Data = a;
}
public String getDATA () {
return Data;
}
public void setDATA(String value) {
this.Data = value;
}
public String toString() {
return "Wassup Yo!!!!-->"+this.Data;
}
}
And then the XML mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
package="org.hibernate.demo">
<class name="Stuffed"
table="STUFFED"
discriminator-value="N">
<id name="DATA" type="string">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
And the Main class:
Code:
//$Id: Main.java,v 1.1.2.6 2003/11/03 09:54:07 oneovthafew Exp $
package org.hibernate.demo;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import net.sf.hibernate.FetchMode;
import net.sf.hibernate.FlushMode;
import net.sf.hibernate.LockMode;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.cfg.Environment;
import net.sf.hibernate.expression.Example;
import net.sf.hibernate.expression.Expression;
import net.sf.hibernate.expression.MatchMode;
/**
* Demonstrate some useful features of Hibernate.
*
* @ripped off from Gavin King
*/
public class Main {
private SessionFactory factory;
public static void main(String[] args) throws Exception {
final Main test = new Main();
Configuration cfg = new Configuration()
.addClass(Stuffed.class)
.setProperty(Environment.HBM2DDL_AUTO, "update");
test.factory = cfg.buildSessionFactory();
Session s = test.factory.openSession();
Transaction tx = s.beginTransaction();
Stuffed st = new Stuffed("hala");
s.saveOrUpdate(st);
List l = s.createQuery("from Stuffed").list();
System.out.println(((Stuffed)l.get(0)).getDATA());
tx.commit();
s.close();
test.factory.close();
}
}
As you guys can already see I am not doing anything impressive here. Having made a few changes to the provided "build.xml" and created a table called "STUFFED" in my database and then populating it with some dummies, I went on to execute an "ant eg" at the command prompt.
It compiles the files and does all the mapping. The query also runs, but it falls short of giving me results. Its as is the table created by me did not have any data at all.
So, my q is: Is that the right way of accessing a table that already exists in a database? The q is very simple, I agree and that's one of the reasons I could not find a straightforward explanation in the docs or the forum.
All I want to do is access already existing data, nothing more...
Cheers,
SD
Quote:
An experienced person is only experienced, because a newbie exists!