I am trying to run this simple application which looks like this:
1) Persistence class: <TestBean.jav>
public class TestBean {
private int patientID;
private String url;
TestBean() {
}
public int getPatientID(){
return patientID;
}
public String getUrl(){
return url;
}
public void setPatientID(int id){
patientID = id;
}
public void setUrl(String url){
this.url = url;
}
} //end of class
2) Mapping file: <TestBean.hbm.xml>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="TestBean" table="demo">
<property name="patientID" column="patient_id"/>
<property name="url"/>
</class>
</hibernate-mapping>
3) Hibernate Conf file: <hibernate.cfg.xml>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://db-mm.com:5432/test</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup
<property name="hbm2ddl.auto">create</property> -->
<mapping resource="TestBean.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4) Hibernate Helper Class: <HibernateUtil.java>
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this thread has none yet
if (s == null) {
s = sessionFactory.openSession();
// Store it in the ThreadLocal variable
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
5) <BeanManager.java>
import org.hibernate.Transaction;
import org.hibernate.Session;
import java.util.List;
public class BeanManager {
public static void main(String[] args) {
BeanManager mgr = new BeanManager();
if (args[0].equals("list")) {
List urls = mgr.listEvents();
for (int i = 0; i < urls.size(); i++) {
TestBean tb = (TestBean) urls.get(i);
System.out.println("URL: " + tb.getUrl());
}
}
HibernateUtil.sessionFactory.close();
}
private void createAndStoreEvent(String url) {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
TestBean tb = new TestBean();
tb.setUrl(url);
session.save(tb);
tx.commit();
HibernateUtil.closeSession();
}
private List listEvents() {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
List result = session.createQuery("from TestBean").list();
tx.commit();
session.close();
return result;
}
}
and finally the <build.xml>
<project name="kashi" default="compile">
<property name="sourcedir" value="${basedir}/src"/>
<property name="targetdir" value="${basedir}/bin"/>
<property name="librarydir" value="${basedir}/lib"/>
<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
</target>
<target name="compile" depends="clean, copy-resources">
<javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries"/>
</target>
<target name="copy-resources">
<copy todir="${targetdir}">
<fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="run" depends="compile">
<java fork="true" classname="BeanManager" classpathref="libraries">
<classpath path="${targetdir}"/>
<arg value="${action}"/>
</java>
</target>
</project>
And when I run this through ANT i get the following error
[java] 14:04:44,375 ERROR XMLHelper:59 - Error parsing XML: XML InputStream
(11) The content of element type "class" must match "(meta*,subselect?,cache?,sy
nchronize*,comment?,(id|composite-id),discriminator?,natural-id?,(version|timest
amp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|an
y|map|set|list|bag|idbag|array|primitive-array|query-list)*,((join*,subclass*)|j
oined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,fil
ter*)".
[java] Initial SessionFactory creation failed.org.hibernate.MappingExceptio
n: Error reading resource: TestBean.hbm.xml
I have all the necessary jar files needed for running Hibernate, I am sure about this because I have other example application running with the same configuration as this one. Also, FYI I am running Hibernate3.0, on Linux box.
Any clue where I could have gone wrong ????
Thnks
|