- Unit Testing
- How do I specify the length of a column for the SchemaExport tool? A unique column? A not-null column?
- I don't like the column type in the generated table creation script!
- How can I embed table export functionality in my application?
- How can I make SchemaExport create InnoDB tables in MySQL
- The SchemaUpdate tool doesn't work
- The hibernate.hbm2ddl.auto=update setting doesn't create indexes
- Hibernate doesn't generate the database indexes I want in the schema!
- Whats the easiest way to configure Hibernate in a plain Java application (without using JNDI)?
- Whats the easiest way to configure Hibernate in a J2EE application (using JNDI)?
- How do I configure Hibernate as a JMX service in JBoss
- How do I use Hibernate in an EJB 2.1 session bean?
- How do I configure logging?
- How do I configure the cache?
- More Help?
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.
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"/> </property>
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.
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 hibernate.hbm2ddl.auto=update 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
How do I use Hibernate in an EJB 2.1 session bean?
- Look up the SessionFactory in JNDI.
- Call getCurrentSession() to get a Session for the current transaction.
- Do your work.
- 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:
- put log4j.jar in your classpath
- 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:
- remove log4j.jar from the classpath
- run under JDK1.4
- 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?
See the Performance Tuning chapter in the reference documentation.