我使用JPA / Hibernate构建了一个Web应用程序。我使用带有LocalContainerEntityManagerFactoryBean
的{{1}}从java包构建我的实体,并为PostgreSQL配置<property name="packagesToScan" value="<pkg>" />
,所以我没有定义的持久化上下文。
我可以使用Spring注释或xml构建具有这些约束的jdbc.datasource.DriverManagerDataSource
吗?
编辑:
我使用以下表单的代码来获取用户登录的数据。在EntityManager
上下文中,Spring Security
始终为空。
EntityManager
EDIT2:添加了上下文配置
@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
}
}
EDIT3:添加了实体管理器工厂配置:
<context:spring-configured />
<context:annotation-config />
<context:component-scan base-package="<BasePackage>" />
我还在<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>
标记中添加了default-autowire="byType"
,在安全认证bean定义中添加了beans
。