gpt4 book ai didi

jakarta-ee - EJB 未注入(inject)导致 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 01:14:15 24 4
gpt4 key购买 nike

我是 Java EE 的新手,并且已经尝试解决这个问题将近一周。
这是我的 session bean 中的代码,其中我得到了 NullPointerException .发现实体管理器没有被注入(inject)。

来自 session bean 的代码:

package session;

import entity.User;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class UserFacade extends AbstractFacade<User> {
@PersistenceContext (unitName = "EXAMPU")
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
return em;
}

public UserFacade() {
super(User.class);
}

public String validateUser(String username, String password) {
System.out.println("in facade");
boolean pwdMatch = false;

System.out.println("in UserInfoFacade validate " + em);
try {
System.out.println("username " + username);
System.out.println("password " + password);

if(em == null) {
System.out.println("em is null");
}
else {
System.out.println("em is not null");
}

List results = em.createNamedQuery("User.findByUserName").setParameter("userName", username).getResultList();

if(results == null) {
System.out.println("results is null");
}
else {
System.out.println("results is not null");
}
System.out.println("Entity manger is open " + em.isOpen());

if (!results.isEmpty())
{

Iterator userList = results.iterator();
while (userList.hasNext())
{
User user = (User) userList.next();
if (user != null)
{
if (user.getPassword().equals(password))
{
pwdMatch = true;
}
else {
pwdMatch = false;
}
}
else {
return "NULL";
}
}
if (pwdMatch) {
return "MATCH";
}
else {
return "NO_MATCH";
}
}
else {
return "NOT_FOUND";
}
} catch (Exception ex) {
//ex.printStackTrace();
System.out.println("Caught exception in UserFacade " + ex.toString());
}
return "NULL";
}


}

持久性.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="EXAMPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>EXAM</jta-data-source>

<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>

托管 bean 的代码:userBean
package managed;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import session.UserFacade;
import javax.ejb.EJB;

@ManagedBean
@SessionScoped
public class userBean {

private String username, password;
private String response="" ;
private UserFacade userFacade;

public userBean() {
this.userFacade = new UserFacade();
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public String getResponse() {
return response;
}

public void setUsername(String username) {
this.username = username;
}

public void setPassword(String password) {
this.password = password;
}

public void setResponse(String response) {
this.response = response;
}

public String validate() {
System.out.println("in validate going to user facade" + username + password);
response = userFacade.validateUser(username,password);
System.out.println("validUser value " + response);
if (response.equals("MATCH")){
return "welcome.xhtml";
}
else
return "index.xhtml";
}
}
}

最佳答案

调用new UserFacade()只会做它在 Java 中总是做的事情:调用 UserFacade 的无参数构造函数类(class)。

要获得对 EJB 的引用,您需要将其注入(inject)托管 bean,或者使用 JNDI 检索对 EJB 的引用。

关于jakarta-ee - EJB 未注入(inject)导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12991286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com