我正在使用以下配置:
我有一个无状态EJB来执行基本的数据库操作。
@Stateless
public class GenericService {
@PersistenceContext(unitName = "VoxFlowPU")
protected EntityManager em;
public <T extends DefaultEntity> T save(T entity) {
if (entity.getId() == null) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
//other methods
}
我正在使用自定义身份验证提供程序来使用Spring Security框架。 我有一个UserDao类,这个类需要执行一个基本的数据库操作,我使用@EJB注释来使用GenericService类。
@Repository
public class UserDao {
@EJB
private GenericService genericService;
public IrisUser loadUserByUsername(final String username) {
Map<String, Object> params = new HashMap<>();
params.put("login", username);
List<IrisUser> users = genericService.findAllBySpecificsParams(IrisUser.class, params);
if (Utility.isCollectionEmpty(users)) {
return null;
} else {
Role r = new Role();
r.setName("ROLE_USER");
List<Role> roles = new ArrayList<>();
roles.add(r);
users.get(0).setAuthorities(roles);
//Caso seja o primeiro login do usuário ele será inserido nas tabelas de relacionamento do sistema
if (!checkIfUserExistsInVoxFlow(username)) {
addUserInVoxFlow(users.get(0));
}
return users.get(0);
}
}
//other methods
}
但是当代码到达该行时:
List<IrisUser> users = genericService.findAllBySpecificsParams(IrisUser.class, params);
GenericService实例不为null,但EntityManager始终为null。 @PersistenceContext没有实例化变量。
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="VoxFlowPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/comp/env/jdbc/voxFlowDatasource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<class>br.com.voxage.voxflow.entities.Team</class>
<class>br.com.voxage.voxflow.entities.User</class>
<class>br.com.voxage.voxflow.entities.Action</class>
<class>br.com.voxage.voxflow.entities.ActionFields</class>
<class>br.com.voxage.voxflow.entities.Comment</class>
<class>br.com.voxage.voxflow.entities.Field</class>
<class>br.com.voxage.voxflow.entities.FieldType</class>
<class>br.com.voxage.voxflow.entities.FieldTypeOption</class>
<class>br.com.voxage.voxflow.entities.Issue</class>
<class>br.com.voxage.voxflow.entities.IssueFieldsValues</class>
<class>br.com.voxage.voxflow.entities.Status</class>
<class>br.com.voxage.voxflow.entities.StatusHistory</class>
<class>br.com.voxage.voxflow.entities.StatusTeamPermission</class>
<class>br.com.voxage.voxflow.entities.StatusUserPermission</class>
<class>br.com.voxage.voxflow.entities.Step</class>
<class>br.com.voxage.voxflow.entities.Workflow</class>
<class>br.com.voxage.voxflow.entities.IrisUser</class>
<class>br.com.voxage.voxflow.entities.IrisTeam</class>
<class>br.com.voxage.voxflow.entities.FileUpload</class>
<properties>
<property name="eclipselink.logging.logger" value="JavaLogger" />
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
Wildfly standalone.xml:
<datasource jndi-name="java:/comp/env/jdbc/voxFlowDatasource" pool-name="jdbc/voxFlowDatasource" enabled="true" use-java-context="true">
<connection-url>jdbc:sqlserver://MACHINE-NAME;databaseName=voxflow;instanceName=SQLEXPRESS</connection-url>
<driver>sqlserver</driver>
<security>
<user-name>*****</user-name>
<password>*****</password>
</security>
</datasource>
我已经尝试了许多我在foruns中找到的不同解决方案,但我所做的一切似乎没有任何影响。