gpt4 book ai didi

java - 为什么我在尝试开始事务时得到 NullPointerException?

转载 作者:行者123 更新时间:2023-11-29 05:21:35 25 4
gpt4 key购买 nike

在我在这一行中得到 NullPointerException 之前

EntityManager manager = emf.createEntityManager();

但我通过在上面添加一行来修复它

emf =  Persistence.createEntityManagerFactory("main");

现在我在这一行中得到 NullPointerException

transaction.begin();

我不明白为什么。我试图从 try catch block 中获取它,但得到了相同的结果。同样在最后我尝试添加 emf.close();但错误没有得到修复。此外,我包括 @Named 注释,因为有人建议我这样做。

import java.io.Serializable;
import javax.inject.Named;
import javax.inject.Inject;
import java.sql.*;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.persistence.TypedQuery ;
import javax.transaction.UserTransaction;
import javax.transaction.RollbackException;
import javax.persistence.criteria.*;
import java.util.List;
import java.util.*;

@Named
public class UserManager implements Serializable{

@PersistenceUnit(unitName="main")
private EntityManagerFactory emf;

@Resource
private UserTransaction transaction;

public UserManager() {}

public UserTransaction getTransaction() { return transaction; }
public void setTransaction(UserTransaction transaction) { this.transaction = transaction; }

public void addUser(User v) throws Exception {
emf = Persistence.createEntityManagerFactory("main");
EntityManager manager = emf.createEntityManager();
boolean commit;
try {
transaction.begin();
manager.joinTransaction();
commit = false;
try {
manager.persist(v);
transaction.commit();
commit = true;
}finally {
if(commit==false){
transaction.rollback();
}
}
}finally {
manager.close();
//emf.close();
}
}



@Named("subBean")
@RequestScoped
public class UserBean implements Serializable{

private List<User> users;
private UserManager vm;
private User v;
private int total;

public UserBean() {
this.total= 0;
users = new ArrayList<User>();
vm = new UserManager();
v = new User();
}

public String addUserAction() throws Exception {
vm.addUser(v);
users = getUsers();
return "submit1";

}

//getters and setters
}

最佳答案

如我的评论所述,问题在于您正在手动创建 UserManager bean 实例,而 CDI 无法注入(inject)变量。此处注明:

@Named("subBean")
@RequestScoped
public class UserBean implements Serializable{
//variable is just declared...
private UserManager vm;

public UserBean() {
//and here you're initializing it manually...
vm = new UserManager();
}

//rest of your code...
}

让 CDI 通过注入(inject)变量来完成它的工作:

@Named("subBean")
@RequestScoped
public class UserBean implements Serializable{
//variable is just declared
//and notify CDI it should inject an instance when creating an instance of the client class
@Inject
private UserManager vm;

public UserBean() {
//no need to initialize it ever
//vm = new UserManager();
}

//rest of your code...
}

通过这样做,您也不需要创建任何应该注入(inject)到您的其他 bean 中的字段,这意味着您不需要手动初始化 emf 字段:

//this line should be commented as well...
//emf = Persistence.createEntityManagerFactory("main");

关于java - 为什么我在尝试开始事务时得到 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24480328/

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