Announcement Announcement Module
Collapse
No announcement yet.
How to configure EntityInterceptor for Hibernate 4? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 07:47 AM.

  • #2
    And why should it? It is about Hibernate NOT JPA... So not sure what you want with the entityManager....

    Comment


    • #3
      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.

      Comment


      • #4
        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();
        	}
        
                .....
        
        }

        Comment


        • #5
          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.

          Comment


          • #6
            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, 11:10 AM.

            Comment

            Working...
            X