-->
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.  [ 4 posts ] 
Author Message
 Post subject: Entity not saving in FlushEntityEventListener
PostPosted: Thu Oct 12, 2006 1:49 pm 
Newbie

Joined: Tue Dec 13, 2005 6:47 pm
Posts: 7
I am trying using Hibernate events instead of interceptor but I am unable to update or save the entity in FlushEntityEventListener

Please advise, I have tried with SaveOrUpdateEventListener which works fine but it get called twice when I call SaveOrUpdate method and after session flush

Is it possible to update properties in FlushEntityEventListener and get saved in database ???


Hibernate 3.2.0.cr1, with spring 2.0-rc3


Code:
public class FlushEntityEventListenerImpl  implements FlushEntityEventListener {

    private Log log = LogFactory.getLog(getClass());
   
    /* (non-Javadoc)
     * @see org.hibernate.event.FlushEntityEventListener#onFlushEntity(org.hibernate.event.FlushEntityEvent)
     */
    public void onFlushEntity(FlushEntityEvent event) throws HibernateException {
        log.debug("Custom FlushEntityEventListenerImpl is called....");

        if (event.getEntity() instanceof AbstractDomainObject) {

            final Object entity = event.getEntity();

            AbstractDomainObject ado = (AbstractDomainObject) entity;

            String who = "testUser";
            Timestamp when =  new Timestamp(System.currentTimeMillis());
            ado.setModifiedBy(who);
            ado.setModifiedDate(when);
           
        }
    }



spring context in sessionFactory
Code:
        <property name="eventListeners">
            <map>
                <entry key="flush-entity">
                    <list>
                        <bean class="com.liquid.ingest.domain.FlushEntityEventListenerImpl"/>
                        <bean class="org.hibernate.event.def.DefaultFlushEntityEventListener"/>
                    </list>
                </entry>
            </map>
        </property>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 5:08 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Gpatwa,

Are you trying to do some auditing or what are you trying to achieve? In my view it is best to keep your business logic out of the internals of which ever frameworks you are using. It seems that you could use some aspect like code or simply be more dilligent in your code...

This is not ment as an answer to your question... just trying to understand your requirements :)

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 5:31 pm 
Newbie

Joined: Tue Dec 13, 2005 6:47 pm
Posts: 7
It is similar to audit logging, but we don't have different table to record audit log information.

What we have in all our table is this 2 column ModifiedBy and ModifiedDate

I am trying to abstract this property outside of domain object, so suppose if some one save or update a domain object he doesn't neet to set ModifiedBy and ModifiedDate property.

And we have association and collection also who has this properties

So I was using hibernate interceptor earlier to update this value to root object and it's collection and association

But I have to declare extra session factory configuration declaration in my spring context which i don't want just duplicate the decalaration of session factory and we have many mapping files

To avoid that I prefer Hibernate Events over interceptor

I am not calling any save or update method in interceptor, the reason I need use session in interceptor is identify a collection of object which is not mapped to any entity

Code:
        <set name="companyAssocs" table="INV_COMPANY_TYPE_ASSN" lazy="false" fetch="join">
            <key column="COMPANY_ID" not-null="true"/>
            <composite-element class="com.liquid.ingest.domain.company.CompanyAssociation">
                <property name="companyRoleCode" column="COMPANY_TYPE_CODE" type="java.lang.String" />
                &AbstractDomainObject;
            </composite-element>
           
        </set>


B'cos this collection is never caught in intercepted or any events, but hibernate generate proper insert and update sql when I add or update object in this collection

So I did some hack not sure , it is correct way to identify collection which is not mapped.

Code:
                if (types[i].isCollectionType()) {
                    Field field = getField(object.getClass(), properties[i]);
                    field.setAccessible(true);
                    Object object2 = (Object)field.get(object);
                    String role = object.getClass().getName()+"."+field.getName();
                    CollectionPersister persister = implr.getCollectionPersister(role);
                    if (persister instanceof BasicCollectionPersister) {
                        handleCollectionInsert(object2, who, when);
                    }

}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 7:45 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Thanks for the explanation. For the modified date I've used database side triggers and they work pretty well. I am sorry but I do not have advice for your specific case. One approach that may work is to define an interface that your audited classes need to implement and then call back into that interface instead of using meta-type information like in 'object.getClass().getName()'. This is just some general advice that may help or not.

Marius


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.