2

I'm using JPA/Hibernate to build a web app. I'm using a LocalContainerEntityManagerFactoryBean with <property name="packagesToScan" value="<pkg>" /> to build my entities from a java package, and a jdbc.datasource.DriverManagerDataSource configured for PostgreSQL, so I don't have a defined persistence context.

Can I build an EntityManager with these constraints, using either Spring annotations or xml?

EDIT: I'm using a code of the following form to get a user data for login. In the Spring Security context the EntityManager is always null.

@Repository
public class UserDao implements UserDetailsService {
    @PersistenceContext
    private EntityManager entityManager;  // With getter and setter

    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
       // Used by spring-security, here entityManager is null
    }

}

EDIT2: Added context configuration

<context:spring-configured />
<context:annotation-config />
<context:component-scan base-package="<BasePackage>" />

EDIT3: Added entity manager factory config:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="default_unit" />
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"></property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
        </bean>
    </property>
    <property name="packagesToScan" value="<package>" />
    <property name="jpaProperties">
        <value>
            hibernate.show_sql=true
            hibernate.format_sql=true
            hibernate.hbm2ddl.auto=create-drop
            hibernate.hbm2ddl.import_files=/loaded-on-startup/import.sql
             <!-- ALSO CHECK IMPORT.SQL -->
        </value>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    /// CONNECTION CONFIG ///
</bean>

I also have added default-autowire="byType" to my beans tag and autowire="byType" to the security authentication bean definition.

Pedro Montoto García
  • 1,672
  • 2
  • 18
  • 38
  • HOw do you call the `loadUserByUsername` method? – Rohit Nov 10 '14 at 13:46
  • I don't do it, Spring Security does it for me, being an authentication provider (that's why I implement UserDetailsService). – Pedro Montoto García Nov 10 '14 at 13:58
  • If it isn't injected it isn't in a context that has the entitymanagerfactory and doesn't have a `context:annotation-config` or `context:component-scan`. If it would there would have been an exception during startup telling you dependencies couldn't be found. – M. Deinum Nov 10 '14 at 14:45
  • Could it be because I'm using ``? All the tags I put above are in the same file, an `mvc-config.xml`. – Pedro Montoto García Nov 10 '14 at 15:22

1 Answers1

1

Yes. Take a look here . You need to provide a datasource and use LocalContainerEntityManagerFactoryBean bean

Edit

It may be because of no transaction available.Read here

Community
  • 1
  • 1
Rohit
  • 2,132
  • 1
  • 15
  • 24