0

i use spring3 with hibernate3 and jsf2 with primefaces..

I have a problem when i try to load data from the db , i get null pointer Exception as the session factory is null !

I think the problem comes from :

when i made my managedBean in the viewscoped it gives me "Not serializable Exception" so i make all the class members implement serialaizable but it also gives me the not serializable Exception but for this class "org.springframework.orm.hibernate3.LocalSessionFactoryBean" , so i made the session factory transient .

after i made the session factory transient the application works fine but if i made ajax request the session factory is null so i have null pointer exception !!!

any help will be appreciated..

thanks inadvance

k.elgohary
  • 403
  • 8
  • 21
  • answer [here](http://stackoverflow.com/questions/11553335/cant-serialize-session-beans-warning-thrown) gives pointer to solve your problem – baba.kabira Jul 19 '12 at 09:44
  • Thanks for yout reply .. but how can i call manually the deserialization hook method from the dao ? – k.elgohary Jul 19 '12 at 23:38
  • they are hook methods and will be called automatically during both serialization and deserialization you dont need to call it, just make sure deserilization hook method is capable of setting instance to proper value. – baba.kabira Jul 20 '12 at 03:46
  • Fantastic , it works well , but i think it's not the best way to do this . every time i need to give referenc i should make new session factory !! – k.elgohary Jul 20 '12 at 13:48

1 Answers1

0

Not really create a new session factory

You are using spring so reference tree must be something

ManagedBean -> Spring Service -> Spring Repository

Your managed beans should have reference to a service layer(Singleton). This reference you should mark as transient and fetch from application context in method hooks.

The service bean will always be available from context and you dont have to recreate any instance, its just you pick it back from context.

As for hibernate, it should be in Repository layer and since you have protected your service layer from serialization Repository will not be serialized and hence no issue as above.

@ViewScoped
public class ManagedBean implements Serializable {

  @Autowired private transient Service service;

}

@Service
public class ServiceImpl implements Service{

 @Autowired private Repository repository;

}

@Repository
public class RepositoryImpl extends HibernateDaoSupport implements Repository {

  //here you can have hibernate session factory injected for dao support
}

Hope this helps !!!!!

baba.kabira
  • 3,111
  • 2
  • 26
  • 37