HIBERNATE JBoss.org
 |  Register  | 
     
News 
About 
   Feature List 
   Road Map 
Documentation 
   Related Projects 
   External Documentation 
Download 
Forum & Mailinglists 
Support & Training 
JIRA Issue Tracking
Wiki Community Area


Hibernate Public Training Courses


Get Hibernate in Action eBook!


JavaWorld 2003 Finalist


Jolt Award 2004 Winner
      
Documentation > FAQs > Hibernate Users FAQ - Miscellaneous

Hibernate Users FAQ - Miscellaneous

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/excluded.properties file inside junit.jar to look like this (it will get rid of all annoying Jakarta issues):

excluded.0=sun.* 
excluded.1=com.sun.* 
excluded.2=org.omg.* 
excluded.3=javax.* 
excluded.4=sunw.* 
excluded.5=java.* 
excluded.6=org.xml.sax.* 
excluded.7=org.w3c.dom.* 
excluded.8=org.apache.log4j.* 
excluded.9=org.apache.commons.*

Fix 2 (Eric Everman)

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


Toolset

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

http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd

you will find it quite well documented.

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"/>
</property>

How can I embed table export functionality in my application?

Hibernate2

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

Hibernate 1.x

Datastore ds= ...;
new cirrus.hibernate.tools.SchemaExport(ds, properties).create(false, true);

How can I make SchemaExport create InnoDB tables in MySQL

Use

--delimiter=type=InnoDB

on the command line. 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

--default-table-type=InnoDB

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.


Configuration

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

Hibernate 1.x

Instantiate a Datastore programmatically and provide your configuration properties in hibernate.properties.

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

Hibernate 1.x

Use Hibernate.configure() and provide your configuration in hibernate.cfg.xml. Or, if your application sever supports JMX, cirrus.hibernate.jmx.HibernateServiceMBean offers a more elegant approach.

How do I configure Hibernate as a JMX service in JBoss

See Using Hibernate with JBoss.

How do I use Hibernate in a session bean?

From setSessionContext()

1. Call Hibernate.configure() (unless you are using JMX)

2. Obtain a JNDI InitialContext

3. look up the SessionFactory in the JNDI context and save it to an instance variable

For a stateless session bean, open a new Session at the start of the business method and close() it at the end.

For a stateful session bean, you may keep an open Session in an instance variable across several related method invocations. Be careful to disconnect() the session at the end of each business method (and reconnect() it at the beginning of the next).

How do I configure logging?

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

http://jakarta.apache.org/commons/logging.html

To configure log4j you need to do two things:

1. put log4j.jar in your classpath

2. put log4j.properties in your classpath

There is an example log4j.properties 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/logging.properties)

How do I configure the cache?

Hibernate uses JCS for its factory-level caching. JCS is configured by placing a properties file named cache.ccf in the classpath. See

http://jakarta.apache.org/turbine/jcs/

Don't use a JCS distributed cache with usage="read-write". (In fact, you shouldn't be using a read-write cache in a multiserver environment anyway.)


More Help?

Where can I get more help?

Forum & Mailinglists

If you have code thats misbehaving, the quickest way to a solution is to provide:

  • your mapping document(s)
  • the code between sessionFactory.openSession() and session.close()
  • a TRACE level Hibernate log (see above for instructions on enabling logging)

Please spend some time to try and isolate the problem to a particular sequence of Hibernate calls and/or mapping elements before posting.

      

coWiki