-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Initial SessionFactory creation failed Error
PostPosted: Wed Jul 27, 2005 2:52 pm 
Beginner
Beginner

Joined: Tue Jul 19, 2005 1:24 pm
Posts: 22
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 3:06 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Exactly what the exception says: Your TestBean.hbm.xml has a parsing error because it doesn't match the DTD. You are missing an 'id' or 'composite-id' tag.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 3:06 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
You missed the <id>-element (resp. <composite-id>-element) in your mapping.

"Mapped classes must declare the primary key column of the database table."
http://www.hibernate.org/hib_docs/v3/re ... aration-id

Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 3:40 pm 
Beginner
Beginner

Joined: Tue Jul 19, 2005 1:24 pm
Posts: 22
sven wrote:
You missed the <id>-element (resp. <composite-id>-element) in your mapping.

"Mapped classes must declare the primary key column of the database table."
http://www.hibernate.org/hib_docs/v3/re ... aration-id

Best regards
Sven


Thanks Sven ..... you made my life easy. It works after I declare the patientID col as primary key ....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.