-->
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.  [ 3 posts ] 
Author Message
 Post subject: hibernate and spring DependencyInjectionInterceptorFactoryBe
PostPosted: Sat Jun 24, 2006 11:23 am 
Newbie

Joined: Sat Jun 24, 2006 11:05 am
Posts: 2
Hi All,

I have a generic module containing class called User which is persisted using hibernate which is working fine. Some Information of the user object is stored as attributes (UserAttribute) so that we can customize it by the client modules.

e.g.
class User {
Map<String,Attribute> userAttributes = new HashMap<String,UserAttribute>();

public Attribute getAttribute(String pKey){
return userAttributes.get(pKey);
}

}

One of the modules I'm developing requires a versions of that class whis is customized to access tha attribute information according to its own needs.

For example i need to access the value of say: 'att1' as obj.getAtt1() instead of obj.getAttribute('att1'); To implement this, I'm planning to use the decerator pattern through the DependencyInjectionInterceptorFactoryBean where i will configure the spring application to return a sub class of User object (which implements the customized behaviour of the user object)

CustomUser extends User{

public String getAtt1(){
return getAttribute('att1').getStringValue();

}

}


I'd like to know whether this will be ok to implement like this (will the persistancve behaviour be un effected) and whether there are any alternatives available.

_________________
Thanks,
Dharshana


Top
 Profile  
 
 Post subject: i solved the problem myself
PostPosted: Sun Jun 25, 2006 12:33 pm 
Newbie

Joined: Sat Jun 24, 2006 11:05 am
Posts: 2
I managed to solve the problem.

If anyone is interested, fllowing is the code:
my custom interceptor class:
/*
* MyInterceptor.java
*
* Created on 25 June 2006, 16:24
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package intercept;

import domain.CustomUser;
import java.io.Serializable;
import java.util.Iterator;
import org.hibernate.CallbackException;
import org.hibernate.EntityMode;
import org.hibernate.Interceptor;
import org.hibernate.Transaction;
import org.hibernate.type.Type;

/**
*
* @author Dharshana
*/
public class MyInterceptor implements Interceptor {

/** Creates a new instance of MyInterceptor */
public MyInterceptor() {
}

public boolean onLoad(Object object, Serializable serializable, Object[] object0, String[] string, Type[] type) throws CallbackException {
return false;
}

public boolean onFlushDirty(Object object, Serializable serializable, Object[] object0, Object[] object1, String[] string, Type[] type) throws CallbackException {
return false;
}

public boolean onSave(Object object, Serializable serializable, Object[] object0, String[] string, Type[] type) throws CallbackException {
return false;
}

public void onDelete(Object object, Serializable serializable, Object[] object0, String[] string, Type[] type) throws CallbackException {

}

public void preFlush(Iterator iterator) throws CallbackException {

}

public void postFlush(Iterator iterator) throws CallbackException {

}

public Boolean isTransient(Object object) {
return null;
}

public int[] findDirty(Object object, Serializable serializable, Object[] object0, Object[] object1, String[] string, Type[] type) {
return null;
}

public Object instantiate(String string, EntityMode entityMode, Serializable serializable) throws CallbackException {
return new CustomUser((Long) serializable);
}

public String getEntityName(Object object) throws CallbackException {
return null;
}

public Object getEntity(String string, Serializable serializable) throws CallbackException {
return null;
}

public void afterTransactionBegin(Transaction transaction) {
}

public void beforeTransactionCompletion(Transaction transaction) {
}

public void afterTransactionCompletion(Transaction transaction) {
}

}

class CustomUser extends User{
public CustomUSer(){}
public CustomUser(Long id){
setId(id);
}

}


in the spring mapping file:

<property name="entityInterceptor"><ref local="dependencyInjectionInterceptor"/></property>
<bean id="dependencyInjectionInterceptor"
class="intercept.MyInterceptor"
>
</bean>


But there is a new problem now:

when saving the data, hibernate throws persistance exception because it cant find the persistor for the derived customuser class, im thinking of extending the sessionimpl.java to get around it.. not sure whether it is going to be a good idea though :)

Fixed the above problem by implementing the public String getEntityName(Object object) throws CallbackException in my interceptor.. So thats done i guess

_________________
Thanks,
Dharshana


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 25, 2006 2:31 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Only one thing to simplify your interceptor : you can use the EmptyInterceptor (adapter pattern) super-class instead of the Interceptor interface. This way your interceptor will be much much shorter, and so will contain only interesting parts and be more maintainable.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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