-->
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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: newbie question
PostPosted: Fri Jan 13, 2006 8:50 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
I am a newbie to hibernate. I am doing the example specified in www.hibernate.org and using oracle as the database.

When I run the program I get the error :

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not read mappings from resource: events/Event.hbm.xml
java.lang.ExceptionInInitializerError
at util.HibernateUtil.<clinit>(Unknown Source)
at events.EventManager.createAndStoreEvent(Unknown Source)
at events.EventManager.main(Unknown Source)
Caused by: org.hibernate.MappingException: Could not read mappings from resource: events/Event.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:484)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1443)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1411)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1392)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1368)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1288)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1274)
... 3 more
Caused by: org.hibernate.MappingException: Could not parse mapping document in input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:430)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:481)
... 9 more
Caused by: org.dom4j.DocumentException: Error on line 2 of document : XML declaration may only begin entities. Nested exception: XML declaration may only begin entities.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:421)
... 10 more
Exception in thread "main"

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

my hibernate.cfg.xml is :

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.Driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@172.17.8.52:1521:IFHT03</property>
<property name="hibernate.connection.datasource"> java:comp/env/jdbc/HibAppDS</property>
<property name="hibernate.connection.username">daweb_user</property>
<property name="hibernate.connection.password">daweb_user</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">select</property>
<property name="hibernate.hbm2ddl.auto">create</property>

<mapping resource="events/Event.hbm.xml"/>
<class-cache class="/events/Event" region="Event" usage="read-write"/>
</session-factory>
</hibernate-configuration>
---------------------------------------------------------------------------------

and Event.hbm.xml is :

<?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="events.Event" table="EVENTS">
<id name="id" column ="EVENT_ID" type="integer"/>
<generator class="native"/>
</id>
<property name="dt" type="timestamp" column="EVENT_DATE"/>
<property name="title" column="TITLE"/>
</class>

</hibernate-mapping>


Can any one help me what causes these errors ?

Thanks,
JSV


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:06 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Event.hbm.xml is foobar

The xml declaration was wrong heance "line 2" error, plus you had closed the class tag early - try this...

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>   
   <class name="events.Event" table="EVENTS">
      <id name="id" column="EVENT_ID" type="integer">
         <generator class="native"/>
      </id>
      <property name="dt" type="timestamp" column="EVENT_DATE"/>
      <property name="title" column="TITLE"/>
   </class>   
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Cannot cache an unknown entity
PostPosted: Fri Jan 13, 2006 9:18 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
Thank you very much for the response. The xml error vanished.

But there comes a new error :

Initial SessionFactory creation failed.org.hibernate.MappingException: Cannot cache an unknown entity: events/Event

Why is this please ?

I have put the Event class in src/events/Event.java


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:22 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
sounds like you don't have a class called events.Event on the classpath

(p-s. when someone solves a problem you give them credit ;o)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:27 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
I have removed this line from the hibernate.cfg.xml.
<class-cache class="events/Event" region="Event" usage="read-write"/>

But now the error became

Caused by: org.hibernate.HibernateException: JDBC Driver class not found: oracle.jdbc.driver.OracleDriver

the driver class is correct. I was using for this for other applications.

Cannt get why this happens ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:31 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
You need classes12.zip or the oracle jar on the classpath


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:46 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
Thank you so much Tim.

But still my problem exists . The new error is

Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
java.lang.ExceptionInInitializerError
at util.HibernateUtil.<clinit>(Unknown Source)
at events.EventManager.createAndStoreEvent(Unknown Source)
at events.EventManager.main(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:1610)
at java.lang.Class.getConstructor0(Class.java:1922)
at java.lang.Class.newInstance0(Class.java:278)
at java.lang.Class.newInstance(Class.java:261)
at org.hibernate.cfg.SettingsFactory.createCacheProvider(SettingsFactory.java:327)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:219)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1152)
... 3 more
Exception in thread "main"

Do I need to give the tag

<class-cache class="events/Event" region="Event" usage="read-write"/>
in the hibernate.cfg.xml ? If then is that tag right ? Where to place it in the xml ?

Please Tim


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:51 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
I have solved the cache problem. I needed ehcache-1.1.jar for that.

Thanks.

but org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:504)
at events.EventManager.createAndStoreEvent(Unknown Source)
at events.EventManager.main(Unknown Source)
Exception in thread "main"

why is this ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:54 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
Do you have ehcache-1.1.jar on your classpath?
It should be in your Hibernate installation somewhere.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:55 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
guess we posted at the same time ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 9:57 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Use this helper class to create the session.

"sessionFactory = new Configuration().configure() .buildSessionFactory();"

You are probably missing the configure() I think

Code:
public class HibernateUtils {

   private static final SessionFactory sessionFactory;

   static {
      try {
         // Create the SessionFactory
         sessionFactory = new Configuration().configure()
               .buildSessionFactory();
      } catch (Throwable ex) {
         // Make sure you log the exception, as it might be swallowed
         throw new ExceptionInInitializerError(ex);
      }
   }

   public static final ThreadLocal session = new ThreadLocal();

   public static Session currentSession() {
      Session s = (Session) session.get();
      // Open a new Session, if this Thread has none yet
      if (s == null) {
         s = sessionFactory.openSession();
         session.set(s);
      }
      return s;
   }

   public static void closeSession() {
      Session s = (Session) session.get();
      if (s != null)
         s.close();
      session.set(null);
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 10:02 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
He forgot to specify the sessioncontextclass in the config file, like
Code:
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 10:08 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
Tim,

My HibernateUtil class is as follows :

package util;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml

sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("indise hibernate util ...");
} 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 SessionFactory getSessionFactory() {
return sessionFactory;
}

}

seems I have given configure()


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 10:08 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Either way will work - never seen your approach. I use Spring...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 10:18 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
jwenting wrote:
He forgot to specify the sessioncontextclass in the config file, like
Code:
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>



That seems working, but i got another set of error :

Hibernate: select hibernate_sequence.nextval from dual
org.hibernate.exception.SQLGrammarException: could not get next sequence value
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:96)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:544)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:533)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:529)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:292)
at $Proxy0.save(Unknown Source)
at events.EventManager.createAndStoreEvent(Unknown Source)
at events.EventManager.main(Unknown Source)
Caused by: java.sql.SQLException: ORA-02289: sequence does not exist

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:168)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:208)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:543)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1405)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:643)
at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:1674)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1870)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:363)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:314)
at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:75)
... 17 more
Exception in thread "main"

need to change the generator class ? i gave it as ' native'


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

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.