Hi!
I want to store some meta data for my entity classes in the database. This meta data
only depends on the entity class, not the entity itself and each entity should have a
reference to the meta data object which describes the class of the entity.
So I have a many-to-one association between a "MetaData" class and some "Entity" classes
which depend on the entity class, only. The name of the entity class is stored in property "classname"
of class "MetaData". Each entity class has a "meta" attribute which holds the associated "MetaData"
object.
MetaData.hbm.xml:
Code:
...
<property name="name" column="name" not-null="true" unique="true"/>
<property name="classname" column="classname" not-null="true" unique="true"/>
<property name="description" column="description"/>
...
In my mapping files for entities I'm using a "many-to-one" tag together with a "formula" tag like this:
In EntityA.hbm.xml:
Code:
...
<many-to-one name="meta" class="MetaData" property-ref="classname">
<formula>'model.EntityA'</formula>
</many-to-one>
...
In EntityB.hbm.xml:
Code:
...
<many-to-one name="meta" class="MetaData" property-ref="classname">
<formula>'model.EntityB'</formula>
</many-to-one>
...
This mapping works if I load entities from the database: the "meta" attribute
gets initialized properly, in my entities I have access to the right MetaData objects.
But it does not work when I create a new entity: the "meta" attribute does
not get initialized, even if I try to reload the entity from the database. I have
to close the session first in order to get access to the association mapped
with the "formula" tag.
Here's some code from my test program:
Code:
...
private static void createEntities()
{
Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
session.saveOrUpdate(new EntityA("Some A Entity", 1234));
session.saveOrUpdate(new EntityB("Some B Entity", "foo"));
// references to MetaData entities are null
listAllEntities("=== List entities (directly after creation)");
// remove all entities from session
session.clear();
// now references to MetaData entities are correctly initialized
listAllEntities("=== List entities (after cleaning Session)");
HibernateUtil.commitTransaction();
}
private static void listAllEntities(String text)
{
System.out.println(text);
listEntities("EntityA");
listEntities("EntityB");
}
private static void listEntities(String type)
{
List<?> res = HibernateUtil.getSession().createQuery("from " + type).list();
for (Object e : res)
System.out.println(e);
}
this produces the following output:
Code:
=== List entities (directly after creation)
EntityA(id=1,name=Some A Entity,value=1234,meta=null)
EntityB(id=1,name=Some B Entity,value=foo,meta=null)
=== List entities (after cleaning Session)
EntityA(id=1,name=Some A Entity,value=1234,meta=MetaData(id=1,name=EntityA,classname=model.EntityA,description=MetaData for EntityA))
EntityB(id=1,name=Some B Entity,value=foo,meta=MetaData(id=2,name=EntityB,classname=model.EntityB,description=MetaData for EntityB))
As you can see, meta is null when listing the entities which were
created in the same session, and meta contains the right value
if listing entities loaded in another session.
Is this expected behavior?
How can I Hibernate force to initialize the associations with the formula tag
on newly created entities in the same session?