Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Hibernate-3.2
Mapping documents:
Event.hbm.xml
<?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">
<generator class="native"/>
</id>
</class>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</hibernate-mapping>
Hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="events/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():
HibernateUtil
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();
} 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;
}
}
EventManager
package events;
import org.hibernate.Session;
import java.util.Date;
import util.HibernateUtil;
public class EventManager {
public static void main(String[] args) {
EventManager mgr = new EventManager();
if (args[0].equals("store")) {
mgr.createAndStoreEvent("My Event", new Date());
}
HibernateUtil.getSessionFactory().close();
}
private void createAndStoreEvent(String title, Date theDate) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
}
}
Full stack trace of any exception that occurs:
Buildfile: build.xml
clean:
[delete] Deleting directory C:\Documents and Settings\ofbarraganpu\Escritorio
\PruebaHibernate\bin
[mkdir] Created dir: C:\Documents and Settings\ofbarraganpu\Escritorio\Prueb
aHibernate\bin
copy-resources:
[copy] Copying 14 files to C:\Documents and Settings\ofbarraganpu\Escritori
o\PruebaHibernate\bin
compile:
[javac] Compiling 3 source files to C:\Documents and Settings\ofbarraganpu\E
scritorio\PruebaHibernate\bin
run:
[java] 14:10:24,602 INFO Environment:499 - Hibernate 3.2 cr2
[java] 14:10:24,617 INFO Environment:532 - hibernate.properties not found
[java] 14:10:24,617 INFO Environment:666 - Bytecode provider name : cglib
[java] 14:10:24,633 INFO Environment:583 - using JDK 1.4 java.sql.Timestam
p handling
[java] 14:10:24,711 INFO Configuration:1345 - configuring from resource: /
hibernate.cfg.xml
[java] 14:10:24,711 INFO Configuration:1322 - Configuration resource: /hib
ernate.cfg.xml
[java] 14:10:25,118 INFO Configuration:502 - Reading mappings from resourc
e: events/Event.hbm.xml
[java] 14:10:25,227 ERROR XMLHelper:61 - Error parsing XML: XML InputStream
(15) The content of element type "hibernate-mapping" must match "(meta*,typedef*
,import*,(class|subclass|joined-subclass|union-subclass)*,resultset*,(query|sql-
query)*,filter-def*,database-object*)".
[java] Initial SessionFactory creation failed.org.hibernate.MappingExceptio
n: Could not read mappings from resource: events/Event.hbm.xml
[java] Exception in thread "main" java.lang.ExceptionInInitializerError
[java] at util.HibernateUtil.<clinit>(Unknown Source)
[java] at events.EventManager.createAndStoreEvent(Unknown Source)
[java] at events.EventManager.main(Unknown Source)
[java] Caused by: org.hibernate.MappingException: Could not read mappings f
rom resource: events/Event.hbm.xml
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.jav
a:518)
[java] at org.hibernate.cfg.Configuration.parseMappingElement(Configura
tion.java:1506)
[java] at org.hibernate.cfg.Configuration.parseSessionFactory(Configura
tion.java:1474)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.jav
a:1453)
[java] at org.hibernate.cfg.Configuration.doConfigure(Configuration.jav
a:1427)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java:
1347)
[java] at org.hibernate.cfg.Configuration.configure(Configuration.java:
1333)
[java] ... 3 more
[java] Caused by: org.hibernate.MappingException: invalid mapping
[java] at org.hibernate.cfg.Configuration.addInputStream(Configuration.
java:458)
[java] at org.hibernate.cfg.Configuration.addResource(Configuration.jav
a:515)
[java] ... 9 more
[java] Caused by: org.xml.sax.SAXParseException: The content of element typ
e "hibernate-mapping" must match "(meta*,typedef*,import*,(class|subclass|joined
-subclass|union-subclass)*,resultset*,(query|sql-query)*,filter-def*,database-ob
ject*)".
[java] at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseExce
ption(Unknown Source)
[java] at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Sour
ce)
[java] at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown S
ource)
[java] at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown S
ource)
[java] at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(U
nknown Source)
[java] at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown
Source)
[java] at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElemen
t(Unknown Source)
[java] at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$Fragmen
tContentDispatcher.dispatch(Unknown Source)
[java] at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDoc
ument(Unknown Source)
[java] at org.apache.xerces.parsers.XML11Configuration.parse(Unknown So
urce)
[java] at org.apache.xerces.parsers.XML11Configuration.parse(Unknown So
urce)
[java] at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
[java] at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Sou
rce)
[java] at org.dom4j.io.SAXReader.read(SAXReader.java:465)
[java] at org.hibernate.cfg.Configuration.addInputStream(Configuration.
java:455)
[java] ... 10 more
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 5 seconds
Name and version of the database you are using:
hsqldb
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: