Throws a MappingNotFoundException in HibernateUtil even though the file is present. The exact error is:
Code:
org.hibernate.MappingNotFoundException: resource: net/ce/common/audit/hibernate/Audit.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:596)
Using Hibernate 3.3.1, MsSQLServer 2000 with 2005 jdbc drivers, Eclipse Ganymede, jBoss.
hibernate.cfg.xml is in WEB-INF/classes. hbm files are in src/net/ce/common/audit/hibernate and were generated from the database using HibernateTools.
HibernateUtil is the standard one from the reference manual:
Code:
public class HibernateUtil {
private static final Logger log = Logger.getLogger(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
}catch(Throwable e){
log.error("Failed to create a SessionFactory from hibernate.cfg.xml", e);
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Hibernate.cfg.xml is nothing special. Audit contains Sets of the other two classes, but to keep things simple for the first test I pass empty sets for the associated tables.
Code:
<hibernate-configuration>
<session-factory name="hibernateFactory">
<!-- Database connection -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="hibernate.connection.password">myPWD</property>
<property name="hibernate.connection.url">jdbc:sqlserver://myHost:1433;DatabaseName=MyDB</property>
<property name="hibernate.connection.username">myUsername</property>
<!-- Other properties -->
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<!-- Mappings used -->
<mapping resource="net/ce/common/audit/hibernate/Audit.hbm.xml" />
<mapping resource="net/ce/common/audit/hibernate/AuditDataIds.hbm.xml" />
<mapping resource="net/ce/common/audit/hibernate/AuditFieldsModified.hbm.xml" />
</session-factory>
</hibernate-configuration>
Generated Audit.hbm.xml:
Code:
<hibernate-mapping>
<class name="net.ce.common.audit.hibernate.Audit" table="audit" schema="dbo" catalog="CE_CUSTOM">
<id name="auditId" type="long">
<column name="audit_id" />
<generator class="assigned" />
</id>
<timestamp name="timestamp" column="timestamp" />
<property name="applicationId" type="string">
<column name="application_id" length="10" not-null="true" />
</property>
<property name="actionId" type="string">
<column name="action_id" length="20" not-null="true" />
</property>
<property name="userId" type="string">
<column name="user_id" length="40" not-null="true" />
</property>
<property name="affectedDataType" type="string">
<column name="affected_data_type" length="20" />
</property>
<set name="auditFieldsModifieds" inverse="true">
<key>
<column name="audit_id" not-null="true" />
</key>
<one-to-many class="net.ce.common.audit.hibernate.AuditFieldsModified" />
</set>
<set name="auditDataIdses" inverse="true">
<key>
<column name="audit_id" not-null="true" />
</key>
<one-to-many class="net.ce.common.audit.hibernate.AuditDataIds" />
</set>
</class>
</hibernate-mapping>
Calling code (skipping the generated DAO to troubleshoot):
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(audit);
session.getTransaction().commit();