Hi all, and thanks in advance to anyone who can clear up my confusion. In my hibernate.cfg.xml file, I have a mapping element that gives the .hbm.xml file for my persistant object, User. However, with this element in the hibernate.cfg.xml file, hbm2ddl throws an exception on it, claiming it cannot find the mapping resource. If I comment that line out, hbm2ddl runs fine, but of course my application does not, as hibernate no longer has a mapping file for the persistant object.
I know I must be doing something wrong if I have to use different versions of hibernate.cfg.xml for generating the schema vs. running the app.
Can anyone tell me what I'm missing? All relevant files are below.
Eric
Hibernate version: 3.1.3
Mapping documents:
hibernate.cfg.xml:
Code:
<?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>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="hibernate.connection.url">jdbc:derby:testdb</property>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Enable Hibernate's automatic session
context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="src/com/pearson/mtx/datamodel/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.hbm.xml
Code:
<?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="com.company.product.datamodel.User" table="users">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="login" type="string" unique="true" not-null="true"/>
<property name="password" type="string" />
<many-to-one name="createdBy" class="com.company.product.datamodel.User" />
<many-to-one name="lastModifiedBy" class="com.company.product.datamodel.User"/>
<property name="creationDate" type="timestamp" />
<property name="modifiedDate" type="timestamp" />
<property name="admin" type="boolean" />
<property name="expiredPassword" type="boolean" />
<property name="savedPaymentInfo" type="boolean" />
<property name="CCExpirationMonth" type="string" />
<property name="CCExpirationYear" type="string" />
<property name="CCHolderName" type="string" />
<property name="CCNumber" type="string" />
<property name="CCType" type="string" />
<property name="mirageEmailAddress" type="string" />
<property name="mirageFirstName" type="string" />
<property name="mirageLastName" type="string" />
<property name="mirageMiddleInitial" type="string" />
<property name="mirageMTxUserNumber" type="string" />
<property name="miragePhoneNumber" type="string" />
<property name="miragePhoneExtension" type="string" />
</class>
</hibernate-mapping>
build.xml
Code:
<project name="prototypes" default="makeschema" basedir=".">
<path id="toolslib">
<path location="./lib/hibernate-tools.jar" />
<path location="./lib/hibernate3.jar" />
<path location="./lib/freemarker.jar" />
<path location="./lib/xml-apis.jar" />
<path location="./lib/antlr-2.7.6rc1.jar" />
<path location="./lib/asm.jar" />
<path location="./lib/cglib-2.1.3.jar" />
<path location="./lib/commons-collections-2.1.1.jar" />
<path location="./lib/commons-logging-1.0.4.jar" />
<path location="./lib/dom4j-1.6.1.jar" />
<path location="./lib/ehcache-1.1.jar" />
<path location="./lib/jdbc2_0-stdext.jar" />
<path location="./lib/jta.jar" />
<path location="./lib/log4j-1.2.11.jar" />
<path location="./lib/derby.jar" />
</path>
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="toolslib" />
<target name="makeschema">
<hibernatetool destdir="./">
<classpath>
<path location="./classes"/>
</classpath>
<configuration configurationfile="hibernate.cfg.xml">
<fileset dir="./src">
<include name="**/*.hbm.xml"/>
</fileset>
</configuration>
<hbm2ddl drop="true" console="true" outputfilename="ddl.sql"/>
</hibernatetool>
</target>
</project>
Code between sessionFactory.openSession() and session.close():Code:
package com.company.product.datamodel;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
/**
* @param args
*/
public static void main(String[] args) {
try {
Session s = sessionFactory.getCurrentSession();
s.beginTransaction();
User u = new User();
u.setLogin("admin");
u.setPassword("password");
java.util.Date d = new java.util.Date();
u.setCreationDate(d);
u.setModifiedDate(d);
u.setAdmin(true);
u.setExpiredPassword(false);
u.setSavedPaymentInfo(false);
s.save(u);
s.getTransaction().commit();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Full stack trace of any exception that occurs:When running "ant makeschema" with hibernate.cfg.xml as shown above:
Code:
D:\Projects\ProtoTypes\build.xml:27: org.hibernate.MappingException: Resource: src/com/pearson/mtx/datamodel/User.hbm.xml not found
at org.hibernate.tool.ant.HibernateToolTask.reportException(HibernateToolTask.java:194)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:163)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
Caused by: org.hibernate.MappingException: Resource: src/com/pearson/mtx/datamodel/User.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:479)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1344)
at org.hibernate.tool.ant.ConfigurationTask.doConfiguration(ConfigurationTask.java:108)
at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:55)
at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:226)
at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hbm2DDLExporterTask.java:45)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:160)
... 11 more
--- Nested Exception ---
org.hibernate.MappingException: Resource: src/com/pearson/mtx/datamodel/User.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:479)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1344)
at org.hibernate.tool.ant.ConfigurationTask.doConfiguration(ConfigurationTask.java:108)
at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:55)
at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:226)
at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hbm2DDLExporterTask.java:45)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:160)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
When I remove the mapping element from hibernate.cfg.xml, hbm2ddl runs perfectly, however my running my Hibernate Test class yields:
Code:
org.hibernate.MappingException: Unknown entity: com.company.product.datamodel.User
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1302)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:89)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.save(Unknown Source)
at com.company.product.datamodel.HibernateTest.main(HibernateTest.java:32)
Name and version of the database you are using: Derby 10.1.3.1