-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: many-to-one with formula problem
PostPosted: Wed Dec 29, 2010 2:13 pm 
Newbie

Joined: Fri Jul 14, 2006 7:32 am
Posts: 9
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?


Top
 Profile  
 
 Post subject: Re: many-to-one with formula problem
PostPosted: Thu Dec 30, 2010 10:21 am 
Newbie

Joined: Fri Jul 14, 2006 7:32 am
Posts: 9
I forgot to mention: I can reproduce this problem with Hibernate versions 3.2.7.GA, 3.3.1.GA, 3.3.2.GA and 3.6.0.Final
Database used was PostgreSQL 8.4.5 with PostgreSQL 8.4 JDBC3 (build 701) driver.

I also tried the various methods provided by the Hibernate Session API to persist the new entities: save(), saveOrUpdate(), merge(), persist()
but they all behave the same (regarding the problem described above, that is): the many-to-one association mapped with the "formula" does
not get initialized when the entity gets persisted and it keeps it null value as long as the entity stays in the persistence context, even if the
application tries to re-load it with a query.

I can use Session.refresh(entity) to force Hibernate to re-load the entity immediately after persisting the entity.
That way the problematic many-to-one association gets initialized correctly, but all other properties and
associations the entity might have get reloaded, too. This is quite inefficient. I'd rather find a way to tell Hibernate to
initialize the problematic "formula" association, only.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.