Announcement Announcement Module
Collapse
No announcement yet.
Hibernate Interceptor and getHibernateTemplate?? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate Interceptor and getHibernateTemplate??

    Hi all,

    If one uses the HibernateInterceptor, then should the sessionFactory be passed to the Business Object implementation?

    If yes, then at runtime I get the LazyInitialization exception, as before.

    If no... then how do I perform CRUD operations because if my DAOImpl is a subclass of HibernateDaoSupport, it requires sessionFactory... else it throws an IllegalArgumentException, with the message that either sessionFactory or HibernateTemplate should be supplied.

    Specifically... what change should I make in the following code....

    Please advise.. Is there a good and detailed article on solving this LazyInitializationException problem in Hibernate+Spring combination. I have seen Karl Baum's Weblog on jroller. Any other pointers.

    These are methods in my Business Object Impl which extends HibernateDaoSupport.

    Code:
    public String addCountry() 
    	{
    		Country country= new Country();
    		
    		country.setLabel("SomeCountry");
    		
    		getHibernateTemplate().save( country);
    		
    		return country.getCountryID().toString();
    	}
    
    	public String addState(String countryID, String label)
    			throws NonExistentParentException 
    	{
    		HibernateTemplate template = getHibernateTemplate();
    		
    		final Country country= (Country) 
    								getHibernateTemplate().load( Country.class, Long.valueOf(countryID) );
    		
    		if( country == null )
    		{
    			throw new NonExistentParentException("The Country identified " +
    					"by the supplied countryID (" + countryID + ") " +
    							"does not exist");
    		}
    		
    		System.out.println("Country Id = " + country.getCountryID() );
    		
    		State stateToBeAdded = new State();
    		
    		stateToBeAdded.setLabel( label );
    		
    		country.addState( stateToBeAdded );
    		
    		template.save( stateToBeAdded );
    		
    		template.update( country);
    		
    	        return stateToBeAdded.getStateID().toString();
    	}

  • #2
    Re: Hibernate Interceptor and getHibernateTemplate??

    Originally posted by vaibhavkhattri
    Hi all,

    If one uses the HibernateInterceptor, then should the sessionFactory be passed to the Business Object implementation?

    If yes, then at runtime I get the LazyInitialization exception, as before.

    If no... then how do I perform CRUD operations because if my DAOImpl is a subclass of HibernateDaoSupport, it requires sessionFactory... else it throws an IllegalArgumentException, with the message that either sessionFactory or HibernateTemplate should be supplied.

    Specifically... what change should I make in the following code....

    Please advise.. Is there a good and detailed article on solving this LazyInitializationException problem in Hibernate+Spring combination. I have seen Karl Baum's Weblog on jroller. Any other pointers.

    These are methods in my Business Object Impl which extends HibernateDaoSupport.

    Code:
    public String addCountry() 
    	{
    		Country country= new Country();
    		
    		country.setLabel("SomeCountry");
    		
    		getHibernateTemplate().save( country);
    		
    		return country.getCountryID().toString();
    	}
    
    	public String addState(String countryID, String label)
    			throws NonExistentParentException 
    	{
    		HibernateTemplate template = getHibernateTemplate();
    		
    		final Country country= (Country) 
    								getHibernateTemplate().load( Country.class, Long.valueOf(countryID) );
    		
    		if( country == null )
    		{
    			throw new NonExistentParentException("The Country identified " +
    					"by the supplied countryID (" + countryID + ") " +
    							"does not exist");
    		}
    		
    		System.out.println("Country Id = " + country.getCountryID() );
    		
    		State stateToBeAdded = new State();
    		
    		stateToBeAdded.setLabel( label );
    		
    		country.addState( stateToBeAdded );
    		
    		template.save( stateToBeAdded );
    		
    		template.update( country);
    		
    	        return stateToBeAdded.getStateID().toString();
    	}
    I have the same problem...one solution is to disable lazy

    Comment

    Working...
    X