Reputation: 1712
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.
Upvotes: 2
Views: 910