Archive for the 'hibernate' Category

26
Feb
11

Hibernate exceptions 0.04 Exception in thread “main” org.hibernate.SessionException: Session was already closed


I received this exception:

Exception in thread "main" org.hibernate.SessionException: Session was already closed

A solution is presented in this post. Another solution would be to modify your code to use this:

HibernateUtil.getSessionFactory().openSession();

the first time, instead of :

HibernateUtil.getSessionFactory().getCurrentSession();

The above solution was found on this stackoverflow answer

26
Feb
11

Hibernate exceptions 0.03 Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: javax/persistence/JoinTable


If you receive this error, Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: javax/persistence/JoinTable, make sure you have the ejb3-persistence.jar added to your classpath.

26
Feb
11

Hibernate-Mapping package attribute


The package attribute in <hibernate-mapping package=”some.package”> is used so that you can write:

<hibernate-mapping package=”h1″>

<class name=”Whatever” table=”WHATEVER”>
<id name=”id” column=”WHATEVER_ID”>
<generator class=”native”/>
</id>
</class>

</hibernate-mapping>

instead of:

<hibernate-mapping>

<class name=”h1.Whatever” table=”WHATEVER”>
<id name=”id” column=”WHATEVER_ID”>
<generator class=”native”/>
</id>
</class>

</hibernate-mapping>

26
Feb
11

Hibernate exceptions 0.02 Exception in thread “main” org.hibernate.HibernateException: No CurrentSessionContext configured!


This will appear if you didn’t define the current_session_context_class property. One possible way of fixing this would be adding:

<property name=”current_session_context_class”>thread</property>

to your hibernate.cfg.xml

26
Feb
11

Hibernate exceptions 0.01 Caused by: org.xml.sax.SAXParseException: The content of element type “session-factory” must match “(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)”.


I received this:


Caused by: org.xml.sax.SAXParseException: The content of element type "session-factory" must match "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)".

Seems to have been caused because I had this in my hibernate.cfg.xml:


<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost</property>
    <property name="hibernate.connection.username">sa</property>
    <mapping class="h1.User" file="" jar="" package="" resource="User.hbm.xml"/>
    <property name="hbm2ddl.auto">update</property>
  </session-factory>
</hibernate-configuration>

It got fixed after I put the properties sequentially, like this:


<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hbm2ddl.auto">update</property>
    <mapping class="h1.User" file="" jar="" package="" resource="User.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

26
Feb
11

start hsqldb with an in memory db


java -cp hsqldb.jar org.hsqldb.Server -database.0 mem:aname




Blog Stats

  • 221,522 hits