Results 1 to 8 of 8

Thread: SpringMVC+Webflow+Security+Hibernate+JSF+Icefaces: problem in Hibernate configuration

  1. #1
    Join Date
    Sep 2012
    Posts
    8

    Question SpringMVC+Webflow+Security+Hibernate+JSF+Icefaces: problem in Hibernate configuration

    Hi everyone, after many tries I made my project work with Spring MVC + Spring WebFlow + Spring Security + JSF + Icefaces.

    My next step is to add persistency via Hibernate.
    I don't want to overload this post, so I'll try to minimize the amount of code I'll publish. If anything is missing, I'll add it upon request.

    Some context for you.
    In the package com.infoone.siglo.entity.repos I have some interfaces annotated by @Repository and extending PagingAndSortingRepository<MyPersistentClass, Long>, e.g. the interface MyPersistentClassRepository.
    MyPersistentClass is a class annotated by @Entity.
    In order to store an instance of MyPersistentClass, in the appropriate controller - which is annotated by @Controller and @Service("service_name") - I invoke myPersistentClassRepoInstance.save(myPersistentCla ssInstance), where myPersistentClassRepoInstance is of type MyPersistentClassRepository with annotation @Autowired.

    repository-config.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <repositories
    	xmlns="http://www.springframework.org/schema/data/jpa"
    	xmlns:repositories="http://www.springframework.org/schema/data/repository"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd"
    	base-package="com.infoone.siglo.entity.repos">
    	<repositories:include-filter type="assignable" expression="org.springframework.data.repository.PagingAndSortingRepository"/>
    </repositories>
    data-access-config.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
    	   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
           xsi:schemaLocation="
           		http://www.springframework.org/schema/beans
    			http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          		http://www.springframework.org/schema/tx
           		http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
           		http://www.springframework.org/schema/context
    			http://www.springframework.org/schema/context/spring-context-3.1.xsd
    			http://www.springframework.org/schema/data/jpa
    			http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    	
    	<!-- HSQL instance -->
    	<tx:annotation-driven />
    	
     	<!-- Drives transactions using local JPA APIs -->
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    		
    	<!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider and a simple in-memory data source populated with test data -->
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="packagesToScan" value="com.infoone.siglo.entity" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    				<property name="generateDdl" value="true" />
    			</bean>
    		</property>
     		<property name="jpaProperties">
    	        <props>
    	            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
    	        </props>
    	    </property>
    	</bean>
    
    	<!-- Deploys a in-memory "booking" datasource populated -->
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/siglo" />
    		<property name="username" value="root" />
    		<property name="password" value="password" />
    	</bean>
    
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    			</props>
    		</property>
    	</bean>
    
    <!-- 	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    		<property name="dataSource" ref="dataSource" />
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean> -->
    </beans>
    When I start the server, I get the following exceptions
    Code:
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/config/data-access-config.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    
    [...etc etc]
    
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/config/data-access-config.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    
    [... etc etc]
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
    	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
    	at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
    	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:268)
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    	... 39 more
    Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
    	at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)
    	at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)
    	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:170)
    	at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:73)
    	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2279)
    	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2275)
    	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    	at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
    	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
    	... 45 more
    I'm using the following environment:
    Server: vFabric 2.7
    JDK: 1.7
    STS: 2.9.2

    Any idea of what is wrong?

    Thank you so much.
    Last edited by MManuel; Sep 23rd, 2012 at 06:11 PM.

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

    Default

    Have you READ the stacktrace?

    When constructing your entitymanager you get an exception..

    Code:
    org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
    Specify the dialect.

    Also why use both Hibernate (plain) and JPA?! Use either one not both...
    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
    Sep 2012
    Posts
    8

    Default

    Quote Originally Posted by Marten Deinum View Post
    Have you READ the stacktrace?

    When constructing your entitymanager you get an exception..

    Specify the dialect.
    And have you READ my config file?

    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    			</props>
    		</property>
    	</bean>
    It is specified.

    Also why use both Hibernate (plain) and JPA?! Use either one not both...
    Where am I using both? For the transaction manager? If I set the Hibernate4 transaction manager, updates and inserts do not work anymore. However, that does not seem to be the problem. In fact, I use JPA transaction manager and Hibernate4 session manager, as specified in the example icefaces-booking from the IceFaces website.


    Anyway, education is always well appreciated, no need to be sharp.
    Last edited by MManuel; Sep 24th, 2012 at 09:45 AM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Yes i have read your config file and I doubt you have read either my post or your own configuration file.

    From your configuration file, the aformentiond EntityManager (I nowhere mentioned the Hibernate SessionFactory!!!).

    Code:
     	<!-- Drives transactions using local JPA APIs -->
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    		
    	<!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider and a simple in-memory data source populated with test data -->
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="packagesToScan" value="com.infoone.siglo.entity" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    				<property name="generateDdl" value="true" />
    			</bean>
    		</property>
     		<property name="jpaProperties">
    	        <props>
    	            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
    	        </props>
    	    </property>
    	</bean>
    Which has no database or dialect specified.

    It is specified.
    So no it isn't specified...


    Where am I using both? For the transaction manager? If I set the Hibernate4 transaction manager, updates and inserts do not work anymore. However, that does not seem to be the problem.
    In your configuration you have both a plain hibernate setup and a JPA setup... If you want to use plain JPA then just use plain JPA and don't add a Hibernate SessionFactory into the mix.

    1. Remove the plain hibernate stuff (HibernateTransactionManager and LocalSessionFactoryBean)
    2. on the HibernateJpaVendorAdapter add the databasePlatform property and as a value use org.hibernate.dialect.MySQLDialect.
    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

  5. #5
    Join Date
    Sep 2012
    Posts
    8

    Default

    The problem has been solved and was not where pointed out Marten Denium, but in the wrong configuration of mysql.

    However, mixing Hibernate and JPA is correct, meaningful and even adviced by Icefaces developers,. whilst the specification of the dialect is not mandatory for the EntityManager.
    Thank you anyway for your guesses and attempts.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default


    However, mixing Hibernate and JPA is correct, meaningful and even adviced by Icefaces developers,. whilst the specification of the dialect is not mandatory for the EntityManager.
    Which IMHO is a crappy advice... Why would you need plain hibernate if you use JPA... I don't really understand nor do I want to understand. (The only reason I can think of is when you are migrating from plain hibernate to JPA).

    I guess the advice they give is to use JPA and use Hibernate as the JPA provider (not using plain Hibernate and JPA)...
    Last edited by Marten Deinum; Sep 25th, 2012 at 01:01 AM.
    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

  7. #7
    Join Date
    Sep 2012
    Posts
    8

    Default

    I thought it was a bad idea, but then I did want to understand and got the real reasons.
    It does make sense what I configured and I suggest it as perfectly working.

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    For me it doesn't make sense and I would love to hear the real reasons... For me it still doesn't make sense that if you want ot use JPA you also have to configure a Hibernate SessionFactory... It only consumes memory doesn't add anything (only complexity)... But that is just me...
    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

Posting Permissions

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