Results 1 to 6 of 6

Thread: How to configure EntityInterceptor for Hibernate 4?

  1. #1
    Join Date
    Jan 2012
    Posts
    4

    Question How to configure EntityInterceptor for Hibernate 4?

    Hi,

    I just migrated from Hibernate 3 => Hibernate 4.

    So I have to replace the org.springframework.orm.hibernate3.* classes in my Spring XML Config with the hibernate4 ones.
    But neither
    Code:
    org.springframework.orm.hibernate4.HibernateTransactionManager
    nor
    Code:
    org.springframework.orm.hibernate4.LocalSessionFactoryBean
    contain the entityInterceptor Property.

    What to do?

    Regards,
    Markus

    P.S. I use the latest Spring 3.1.0.RELEASE and Hibernate 4.0.1
    Last edited by pappnas; Jan 25th, 2012 at 07:47 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    And why should it? It is about Hibernate NOT JPA... So not sure what you want with the entityManager....
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2012
    Posts
    4

    Default

    Ah, sorry, I mean EntityInterceptor, not EntityManager, I just corrected the post

    In the hibernate3 package both classes had the entityInterceptor Property and I need to migrate to hibernate4.

  4. #4
    Join Date
    Jan 2012
    Posts
    4

    Default

    I copied the code of LocalSessionFactoryBean, added an entityIntercepter Setter and set the interceptor in the afterPropertiesSet() method. Why not re-add the entityIntercepter Property in the original Spring Implementation?

    Regards,
    Markus

    Code:
    public class LocalSessionFactoryBean implements FactoryBean<SessionFactory>, ResourceLoaderAware,
    		InitializingBean, DisposableBean
    {
            ...
    
    	private SessionFactory			sessionFactory;
    
    	private Interceptor entityInterceptor;
    
    	public void setEntityInterceptor(Interceptor entityInterceptor)
    	{
    		this.entityInterceptor = entityInterceptor;
    	}
    
    
            ....
    
    	@Override
    	public void afterPropertiesSet() throws IOException
    	{
    		LocalSessionFactoryBuilder sfb = new LocalSessionFactoryBuilder(this.dataSource, this.resourcePatternResolver);
    
    		if (this.configLocations != null) {
    			for (Resource resource : this.configLocations) {
    				// Load Hibernate configuration from given location.
    				sfb.configure(resource.getURL());
    			}
    		}
    
    		if (this.mappingResources != null) {
    			// Register given Hibernate mapping definitions, contained in resource files.
    			for (String mapping : this.mappingResources) {
    				Resource mr = new ClassPathResource(mapping.trim(), this.resourcePatternResolver.getClassLoader());
    				sfb.addInputStream(mr.getInputStream());
    			}
    		}
    
    		if (this.mappingLocations != null) {
    			// Register given Hibernate mapping definitions, contained in resource files.
    			for (Resource resource : this.mappingLocations) {
    				sfb.addInputStream(resource.getInputStream());
    			}
    		}
    
    		if (this.cacheableMappingLocations != null) {
    			// Register given cacheable Hibernate mapping definitions, read from the file system.
    			for (Resource resource : this.cacheableMappingLocations) {
    				sfb.addCacheableFile(resource.getFile());
    			}
    		}
    
    		if (this.mappingJarLocations != null) {
    			// Register given Hibernate mapping definitions, contained in jar files.
    			for (Resource resource : this.mappingJarLocations) {
    				sfb.addJar(resource.getFile());
    			}
    		}
    
    		if (this.mappingDirectoryLocations != null) {
    			// Register all Hibernate mapping definitions in the given directories.
    			for (Resource resource : this.mappingDirectoryLocations) {
    				File file = resource.getFile();
    				if (!file.isDirectory()) {
    					throw new IllegalArgumentException(
    							"Mapping directory location [" + resource + "] does not denote a directory");
    				}
    				sfb.addDirectory(file);
    			}
    		}
    
    		if (this.namingStrategy != null) {
    			sfb.setNamingStrategy(this.namingStrategy);
    		}
    
    		if (this.hibernateProperties != null) {
    			sfb.addProperties(this.hibernateProperties);
    		}
    
    		if (this.annotatedClasses != null) {
    			sfb.addAnnotatedClasses(this.annotatedClasses);
    		}
    
    		if (this.annotatedPackages != null) {
    			sfb.addPackages(this.annotatedPackages);
    		}
    
    		if (this.packagesToScan != null) {
    			sfb.scanPackages(this.packagesToScan);
    		}
    
    		if (this.entityInterceptor != null) {
    			sfb.setInterceptor(this.entityInterceptor);
    		}
    
    		this.sessionFactory = sfb.buildSessionFactory();
    	}
    
            .....
    
    }

  5. #5
    Join Date
    Dec 2011
    Posts
    2

    Default

    I am facing the same problem. It is proving difficult to find documentation on how to solve this other than creating my own LocalSessionFactoryBean as Markus has done.

  6. #6
    Join Date
    Apr 2009
    Posts
    14

    Default

    I've added a ticket for this: https://jira.springsource.org/browse/SPR-9987

    As for pappnas's code - it appears to be adding the interceptor to the sessionFactory, not to the session.
    Last edited by wild_oscar; Nov 13th, 2012 at 11:10 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •