Version 1

    Miscellaneous FAQs


    Unit Testing

    I get a ClassCastException (or another "weird" exceptioin) when I try to call Hibernate from inside JUnit.

    Fix 1 (Gareth Cronin)

    Anyone using log4j/commons and JUnit should change the junit/runner/ file inside junit.jar to look like this (it will get rid of all annoying Jakarta issues):




    Fix 2 (Eric Everman)

    Another fix for this is to turn off class reloading in JUnit.



    How do I specify the length of a column for the SchemaExport tool? A unique column? A not-null column?

    Column lengths and constraints may be specified in the mapping document. If you browse the mapping DTD you will find it quite well documented. There is also a chapter about it in the reference documentaiton.


    I don't like the column type in the generated table creation script!

    You can customize the column type using the <column> element, eg.


    <property name="amount" type="big_decimal">
        <column sql-type="NUMERIC(11, 2)" not-null="true"/>


    How can I embed table export functionality in my application?


    Configuration cfg = ...;
    new org.hibernate.tool.hbm2java.SchemaExport(cfg).create(true, true);


    How can I make SchemaExport create InnoDB tables in MySQL





    in the hbm2ddl configuration. Note that it will create a proper script file but the DDL execution statements done by SchemaExport won't use the delimiter, and thus won't create the table as InnoDB.


    Rod Cope

    If you're okay with having all table creation default to type=InnoDB when not otherwise specified, you can start your MySQL server with




    The SchemaUpdate tool doesn't work

    SchemaUpdate relies upon the JDBC metadata API, which is implemented badly, inconsistently, or not at all by JDBC driver vendors. We have found it very difficult to make this tool work on all platforms at once.


    The setting doesn't create indexes

    SchemaUpdate is activated by this configuration setting. SchemaUpdate is not really very powerful and comes without any warranties. For example, it does not create any indexes automatically. Furthermore, SchemaUpdate is only useful in development, per definition (a production schema is never updated automatically). You don't need indexes in development.


    Hibernate doesn't generate the database indexes I want in the schema!

    Automatic schema export (and update) by Hibernate tools is only useful in development. You never need indexes in development, they are purely for performance and scalability tuning in production systems. Production schemas are never automatically generated, at least not completely. A DBA adds indexes to the automatically generated schema during SQL tuning and testing of the application, before going into production with the (possibly automatically generated) base schema, and her handwritten optimized DDL. Also note that optimized DDL is highly vendor specific and totally dependent on the environment (SQL execution plans, tablespace configuration, caches, etc). Even if Hibernate developers would encourage you to automatically generate production-ready schemas (we don't, and we also don't like ad-hoc SQL tuning by throwing a bunch of indexes onto a schema), Hibernate could never offer such a feature.



    Whats the easiest way to configure Hibernate in a plain Java application (without using JNDI)?

    Build a SessionFactory from a Configuration object. See the tutorials in the reference documentation.


    Whats the easiest way to configure Hibernate in a J2EE application (using JNDI)?

    Build a SessionFactory from a Configuration object. See the tutorials in the reference documentation. Specify a name for the SessionFactory in the configuration file, so it will be bound automatically to this name in JNDI.


    How do I configure Hibernate as a JMX service in JBoss

    See Using Hibernate with JBoss.


    How do I use Hibernate in an EJB 2.1 session bean?

    1. Look up the SessionFactory in JNDI.
    2. Call getCurrentSession() to get a Session for the current transaction.
    3. Do your work.
    4. Don't commit or close anything, let the container manage the transaction.


    How do I configure logging?

    Hibernate uses the Apache commons-logging abstraction layer to support whichever logging framework you hapen to be using. See



    To configure log4j you need to do two things:

    1. put log4j.jar in your classpath
    2. put in your classpath


    There is an example in the hibernate-x.x directory. Just change INFO to DEBUG to see more messages (and move it into the classpath).


    To use JDK1.4 logging, do three things:

    1. remove log4j.jar from the classpath
    2. run under JDK1.4
    3. configure logging via the properties file specified by the java.util.logging.config.file system property (this property defaults to $JAVA_HOME/jre/lib/


    How do I configure the cache?

    See the Performance Tuning chapter in the reference documentation.


    More Help?

    Where can I get more help?

    Forum & Mailinglists