I develop an application deployed as a .war file in JBoss 3.2.4RC2, using Hibernate 2.1.3 and MS SQL jdbc drivers towards MS SQL server 2000.
The hibernate mapping is done in a JBoss .sar file.
Previously I deployed all my source code inside of the .war file, but parts of the code is quite generic, and was refactored out into a separate project, and placed in a separate 'common' -jar file.
Then the problems started, first I tried to include the 'common' library inside of the .war file, but I got this exception:
ERROR [Configuration] Could not compile the mapping document
net.sf.hibernate.MappingException: persistent class [no.cronus.forit.hibernate.Users] not found
- I had now rewritten the hibernate mapping class to implement an interface in common.jar, so I figured that common.jar had to be in jboss classpath during statrup:
- Then I put the common.jar file into jboss\server\default\lib instead, and at least JBoss startup now went without errors being logged...
Then som info on my mapping (changed info is bold):
Code:
<hibernate-mapping>
<class name="no.cronus.forit.hibernate.Users" table="Users">
<meta attribute="class-description" inherit="false">
@hibernate.class table="Users"
</meta>
<!-- Added -->
<meta attribute="implements" inherit="false">no.cronus.common.value.User</meta>
<!-- /Added -->
<id name="userId" type="java.lang.Integer" column="UserId" unsaved-value="0">
<meta attribute="field-description">
@hibernate.id generator-class="identity" type="java.lang.Integer" column="UserId" unsaved-value="0"
</meta>
<generator class="identity" />
</id>
<property name="userName" type="java.lang.String" column="UserName" not-null="true" unique="true" length="40">
<meta attribute="field-description">
@hibernate.property column="UserName" length="40" not-null="true"
</meta>
</property>
<property name="userPassword" type="java.lang.String" column="UserPassword" not-null="true" length="100">
<meta attribute="field-description">
@hibernate.property column="UserPassword" length="100" not-null="true"
</meta>
</property>
<property name="createdBy" type="java.lang.Integer" column="CreatedBy" length="10">
<meta attribute="field-description">
@hibernate.property column="CreatedBy" length="10"
</meta>
</property>
<property name="createdDate" type="java.sql.Timestamp" column="CreatedDate" length="23">
<meta attribute="field-description">
@hibernate.property column="CreatedDate" length="23"
</meta>
</property>
<property name="modifiedBy" type="java.lang.Integer" column="ModifiedBy" length="10">
<meta attribute="field-description">
@hibernate.property column="ModifiedBy" length="10"
</meta>
</property>
<property name="modifiedDate" type="java.sql.Timestamp" column="ModifiedDate" length="23">
<meta attribute="field-description">
@hibernate.property column="ModifiedDate" length="23"
</meta>
</property>
<property name="voided" type="java.lang.Short" column="Voided" length="5">
<meta attribute="field-description">
@hibernate.property column="Voided" length="5"
</meta>
</property>
<!-- associations -->
<!-- bi-directional one-to-many association to UserAccess -->
<set name="userAccess" lazy="true" inverse="true">
<meta attribute="field-description">
@hibernate.set lazy="true" inverse="true"
@hibernate.collection-key column="UserId"
@hibernate.collection-one-to-many class="no.cronus.forit.hibernate.UserAccess"
</meta>
<key><column name="UserId" /></key>
<one-to-many class="no.cronus.forit.hibernate.UserAccess"/>
</set>
</class>
</hibernate-mapping>
On the Java side I changed my hardcoded reference to the Hibernate mapping class, to one that is decided runtime in this method:
Code:
public static Class getHibernateMappingClass() {
Class o = null;
String className = Constants.getProperty("hibernate.mapping.class");
if (className != null) {
try {
o = Class.forName(className);
} catch (Exception e) {
log.fatal("Could not load Hibernate mapping class.", e);
}
}
return o;
}
Java code that access the database via Hibernate:
Code:
...
Session session = HibernateUtil.getSession();
/*
Before:
Criteria criteria = session.createCriteria(myapp.hib.Users.class);
*/
Criteria criteria = session.createCriteria(getHibernateMappingClass());
Object result;
if (mUserId != null) {
criteria.add(Expression.eq("userId", mUserId));
result = criteria.list()!=null && criteria.list().size()>0 ? criteria.list().get(0) : null;
} else {
criteria.addOrder(orderAscending
? Order.asc(getOrderByColumn())
: Order.desc(getOrderByColumn()));
if (!includeVoidedRecords) criteria.add(Expression.or(Expression.eq("voided", new Short("0")), Expression.isNull("voided")));
if (mUserName!=null) criteria.add(Expression.eq("userName", mUserName));
result = criteria.list();
}
session.flush();
session.close();
return result;
Hibernate debug logging:
Code:
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionFactoryObjectFactory] JNDI lookup: HibernateFactory
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionFactoryObjectFactory] lookup: uid=d1801936fc9cd0b800fc9cd0cb350000
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] opened session
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] flushing session
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushing entities and processing referenced collections
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Processing unreferenced collections
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Scheduling collection removes/(re)creates/updates
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Dont need to execute flush
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] flushing session
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushing entities and processing referenced collections
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Processing unreferenced collections
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Scheduling collection removes/(re)creates/updates
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] executing flush
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] post flush
2004-05-19 12:36:02,450 DEBUG [net.sf.hibernate.impl.SessionImpl] closing session
2004-05-19 12:36:21,108 DEBUG [net.sf.hibernate.impl.SessionFactoryObjectFactory] JNDI lookup: HibernateFactory
2004-05-19 12:36:21,108 DEBUG [net.sf.hibernate.impl.SessionFactoryObjectFactory] lookup: uid=d1801936fc9cd0b800fc9cd0cb350000
2004-05-19 12:36:21,108 DEBUG [net.sf.hibernate.impl.SessionImpl] opened session
2004-05-19 12:36:21,118 DEBUG [net.sf.hibernate.impl.SessionImpl] flushing session
2004-05-19 12:36:21,118 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushing entities and processing referenced collections
2004-05-19 12:36:21,118 DEBUG [net.sf.hibernate.impl.SessionImpl] Processing unreferenced collections
2004-05-19 12:36:21,118 DEBUG [net.sf.hibernate.impl.SessionImpl] Scheduling collection removes/(re)creates/updates
2004-05-19 12:36:21,118 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2004-05-19 12:36:21,118 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2004-05-19 12:36:21,118 DEBUG [net.sf.hibernate.impl.SessionImpl] Dont need to execute flush
2004-05-19 12:36:21,118 DEBUG [net.sf.hibernate.impl.SessionImpl] flushing session
2004-05-19 12:36:21,118 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushing entities and processing referenced collections
2004-05-19 12:36:21,128 DEBUG [net.sf.hibernate.impl.SessionImpl] Processing unreferenced collections
2004-05-19 12:36:21,128 DEBUG [net.sf.hibernate.impl.SessionImpl] Scheduling collection removes/(re)creates/updates
2004-05-19 12:36:21,128 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
2004-05-19 12:36:21,128 DEBUG [net.sf.hibernate.impl.SessionImpl] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2004-05-19 12:36:21,128 DEBUG [net.sf.hibernate.impl.SessionImpl] executing flush
2004-05-19 12:36:21,128 DEBUG [net.sf.hibernate.impl.SessionImpl] post flush
2004-05-19 12:36:21,128 DEBUG [net.sf.hibernate.impl.SessionImpl] closing session
The database table contains these records:
Code:
...
7;oper;-54-7d-38-1c3-2b-3b-10-7e-1d731a4f-5e7e7f-1635-7e22;<NULL>;2004-04-23 18:59:21.733;<NULL>;<NULL>;0;
...
After changing to this more dynamic behaviour, Hibernate just decides that this record does not exist in the database, and does not return any records when queried!