Spring + JPA + Hibernate

26,182

Solution 1

I think you're missing the configuration to tell spring to search for the annotations (add the following anywhere between the <beans> element:

<context:annotation-config />

And maybe you also need to add the following specifying the package where you have the DAO. But I don't think this is required.

<context:component-scan base-package="your.package" />

Solution 2

this is working piece of code:

Spring.xml:

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
        <context:annotation-config />
        <context:component-scan base-package="com.package.dao" />
        <tx:annotation-driven transaction-manager="transactionManager" />
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
                <property name="persistenceUnitName" value="fcmsServer" />
        </bean>
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
                <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>        
</beans> 

Dao bean:

@Repository
public class UserDaoBean implements UserDao {
        @PersistenceContext
        protected EntityManager em;         
}

Persistence.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_1_0.xsd"
             version="1.0">
    <persistence-unit name="fcmsServer" transaction-type="RESOURCE_LOCAL">

        <class>com.package.entity.</class>
        <properties>
            <property name="javax.persistence.jdbc.url"
                 value="jdbc:mysql://localhost:3306/db_name"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="admin"/>
            <property name="javax.persistence.jdbc.password" value="admin"/>
        </properties>
    </persistence-unit>
</persistence>
Share:
26,182
Mikhail S
Author by

Mikhail S

Updated on April 12, 2020

Comments

  • Mikhail S
    Mikhail S about 4 years

    I am newbie in Spring. I am trying to setup simple web application with Spring 3.1.3 and JPA 2.0 I have added all necessary libs to the WEB-INF/lib. There is no errors during the startup but entityManager in my DaoImpl file is null. So, this is my configuration:

    persistance.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0"
    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">
    
    <persistence-unit name="fcmsServer" transaction-type="RESOURCE_LOCAL">
    </persistence-unit>
    </persistence>
    

    fcms-servlet.xml

    <?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!-- the application context definition for the fcmsServer DispatcherServlet -->
    
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="fcmsServer" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
            </bean>
        </property>
        <property name="persistenceUnitManager">
            <bean
                class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
                <property name="defaultDataSource" ref="dataSource" />
            </bean>
        </property>
    </bean>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:tcp://localhost/~/fcms" />
        <property name="username" value="sa" />
        <property name="password" value="sa" />
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <bean id="persistenceAnnotation"
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
    
    <bean id="userDao" class="fcms.data.user.UserDAOImpl">
    </bean>
    
    <bean name="/user.htm" class="fcms.controller.UserController">
    </bean>
    
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    

    DaoImpl class:

    @Repository
    public class UserDAOImpl implements UserDAO {
    @PersistenceContext(unitName = "fcmsServer")
    private EntityManager entityManager;
    
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    
    @Override
    public User getUserById(long id) {
        return entityManager.find(User.class,id);
    }
    
    @Override
    public void addUser(User u) {
        entityManager.persist(u);
    }
    }
    

    User class

    @Entity
    @Table(name = "User")
    public class User implements Serializable {
    
    
    @Id  @GeneratedValue
    private long id;
    @Column(name = "lastName", nullable = false)
    private String lastName;
    @Column(name = "firstName", nullable = false)
    private String firstName;
    @Column(name = "birthDate", nullable = true)
    private Date birthDate;
    private static final long serialVersionUID = 1L;
    

    }

    So when I try to call addUser() in UserController, I have NullPointerException during persist.

  • Mikhail S
    Mikhail S about 11 years
    Unfortunatelly, this solution does not fix the problem. entityManager is still equeal to null.
  • Ferda-Ozdemir-Sonmez
    Ferda-Ozdemir-Sonmez over 7 years
    Hi, I am also defining an entitymanagerfactory like your ut I am not sure what should I have to give to the persistence-unit name="fcmsServer". What does it refer to?
  • bvyas
    bvyas almost 7 years
    @Ferda-Ozdemir-Sonmez : persistence-unit name is just for unique identiier of your entityManagerFactory. This is helpful when any application is configure more than one database/dataSource. You can give any name.