0

I have Spring MVC project. I am using HSQL database and Hibernate. I have got stuck during making connection with Spring MVC (JPA as default) and Hibernate. I have created persistance.xml file in Spring MVC directory: src/main/resources/META-INF. I have also tried to place it in Spring MVC directory: Deployed Resources/webapp/WEB-INF/classes/META-INF. I don't know which one is correct. I have still the same problem.

This is my persistance.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> 
<persistence-unit name="entityManager">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>package.pl.models.Users</class>
    <properties>                    
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:/home/user/hsqldb/lib/my.spring.mvc/mybase" />
        <property name="javax.persistence.jdbc.user" value="user" />
        <property name="javax.persistence.jdbc.password" value="password" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />          
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
    </properties>
</persistence-unit>

This is one of my Spring MVC controller method body:

UsersHome usersHome = new UsersHome();
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("entityManager");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
usersHome.setEntityManager(entityManager);
Users user = usersHome.findByUsername(login);

I have received error on the line when I have used EntityManagerFactory for the first time - indicating the name of my Persistence Unit.

My exception is:

org.springframework.web.util.NestedServletException: Request processing failed; 
nested exception is javax.persistence.PersistenceException: No Persistence 
provider for EntityManager named entityManager

And the root cause is:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named entityManager

I have used appropriate name in EntityManagerFactory. Presumably I have done mistake in my Dispatcher Servlet Context file. I have this code according to Hibernate etc.:

<!-- Hibernate session factory -->      
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">     

    <beans:property name="dataSource">
        <beans:ref bean="dataSource" />
    </beans:property>

    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
            <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
        </beans:props>
    </beans:property>

    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>package.pl.models.Users</beans:value>                              
        </beans:list>
    </beans:property>       
</beans:bean>
<!-- Hibernate session factory end -->

<!-- <beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <beans:property name="persistenceUnitName" value="entityManager" />
</beans:bean> -->

<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>   

<!-- <beans:bean id="usersDao" class="hutter.pl.dao.UsersHome">
    <beans:property name="entityManagerFactory" ref="entityManagerFactory" />
</beans:bean> -->

I have commented the bean id="entityManagerFactory" and bean id="usersDao". Are the necessary? However I can't start my web application with uncommented bean id="entityManagerFactory". I am receiving this exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with 
name 'entityManagerFactory' defined in ServletContext resource 
[/WEB-INF/spring/appServlet/servlet-context.xml]: Invocation of init method failed;
nested exception is javax.persistence.PersistenceException: No Persistence 
provider for EntityManager named entityManager
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
woyaru
  • 5,544
  • 13
  • 54
  • 92

2 Answers2

2

Sorry, I have not read your full question. I stopped when I saw this: persistance.xml. What about using persistence.xml?

jpkroehling
  • 13,881
  • 1
  • 37
  • 39
  • I would like to use Spring MVC with Hibernate. As far as I know Spring offers few object relational mappings. I am reading now Spring Reference http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/orm.html and I am wondering if I have mixed JPA and Hibernate here. I have thought that Spring MVC uses JPA as default and I have to use Hibernate and JPA together or Hibernate as API for JPA... – woyaru Jul 10 '12 at 09:25
  • I have generated by Hibernate some classes and some of them with `Home` ending are using `@PersistanceContext EntityManager entityManager` so I have thought that I have to handle Persistance Unit. – woyaru Jul 10 '12 at 09:28
  • Not sure how your comment relates to the answer... Did it work? Did you renamed it to "persistence.xml", (note the "e" instead of "a")? – jpkroehling Jul 10 '12 at 12:32
  • OK I haven't been sleeping long tonight. It can't be true. I have written persistance insted of persistence... – woyaru Jul 10 '12 at 13:35
  • Thanks a million of course. In addition: yes, it works. I have occured another exception `The application must supply JDBC connections`. I must search now something about that. – woyaru Jul 10 '12 at 13:48
0

Put your xml file into WEB-INF folder. The xml file contents sessionFactory and all that stub. And import it into spring.xml file as follows:

<import resource="classpath:persistance.xml" />
Sachin J
  • 2,081
  • 12
  • 36
  • 50