gpt4 book ai didi

java - 如何修复Java中的LazyInitializationException?

转载 作者:行者123 更新时间:2023-12-01 19:41:10 25 4
gpt4 key购买 nike

我从事一个小项目,有 2 个表:用户和应用程序。一个用户可以拥有多个应用程序,一个应用程序也可能被多个用户使用,因此它们之间是多对多的关系。每个表都有一些字段(id、名称、密码、技术等),我还在 User 和 Application 类中使用 @ManyToMany 注释声明了 2 个数组列表。问题是,在我的业务层中,我编写了一个应该向用户添加应用程序的方法,当我尝试执行 user.getListOfApplications().add(app) 时,它给了我这个异常......

公共(public)类ManagerHibernate{ 私有(private) SessionFactory sessionFactory;

public void setup()
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}

public void exit()
{
sessionFactory.close();
}

public void create(Object obj)
{
Session session = sessionFactory.openSession();
session.beginTransaction();

session.save(obj);

session.getTransaction().commit();
session.close();
}

public Object read(Class<?> c, int idObj)
{
Session session = sessionFactory.openSession();
session.beginTransaction();

Object obj = session.get(c, idObj);

System.out.println(obj);

session.getTransaction().commit();
session.close();
return obj;
}

public void update(Object obj)
{
Session session = sessionFactory.openSession();
session.beginTransaction();

session.update(obj);

session.getTransaction().commit();
session.close();
}

public void delete(Object obj)
{
Session session = sessionFactory.openSession();
session.beginTransaction();

session.delete(obj);

session.getTransaction().commit();
session.close();
}

public <T> List<T> loadAllData(Class<T> type)
{
Session session = sessionFactory.openSession();
session.beginTransaction();

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<T> criteria = builder.createQuery(type);
criteria.from(type);
List<T> data = session.createQuery(criteria).getResultList();

session.getTransaction().commit();
session.close();
return data;
}

}

public Boolean addNewApplicationToUser(String userUserName, String applicationName)
{
int okUser = 0;
int okApp = 0;

listOfApplications = managerHibernate.loadAllData(Application.class);
listOfUsers = managerHibernate.loadAllData(User.class);
User user = null;
Application app = null;

for(Application index: listOfApplications)
{
if(index.getApplicationName().equals(applicationName))
{
okApp = 1;
app = index;
}
}

for(User index: listOfUsers)
{
if(index.getUserUserName().equals(userUserName))
{
okUser = 1;
user = index;
}
}

if(okUser == 0 || okApp == 0)
return false;
else
{
user.getListOfApplications().add(app);
//app.getUserList().add(user);
return true;
}
}

addNewApplicationToUser 方法是在另一个名为 ControllerHibernate 的类中编写的。只有else分支很重要,剩下的就是检查参数是否确实存在于数据库中

最佳答案

当您使用以下方法加载数据时就会出现问题ma​​nagerHibernate.loadAllData

public <T> List<T> loadAllData(Class<T> type)
{
// New session was opened here
Session session = sessionFactory.openSession();
session.beginTransaction();

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<T> criteria = builder.createQuery(type);
criteria.from(type);
List<T> data = session.createQuery(criteria).getResultList();

session.getTransaction().commit();
session.close();
//session is close here
return data;
}

因此,当您加载数据时,hibernate 框架将仅加载用户对象。由于您选择在模型类中使用延迟加载,因此仅当您尝试访问列表时才会加载应用程序值。由于您已经关闭了 session ,框架无法再获取应用程序列表,从而导致延迟加载异常。

listOfApplications = managerHibernate.loadAllData(Application.class);
//loading user data and close the session associated with it
listOfUsers = managerHibernate.loadAllData(User.class);
User user = null;
Application app = null;

for(Application index: listOfApplications)
{
if(index.getApplicationName().equals(applicationName))
{
okApp = 1;
app = index;
}
}

for(User index: listOfUsers)
{
if(index.getUserUserName().equals(userUserName))
{
okUser = 1;
user = index;
}
}

if(okUser == 0 || okApp == 0)
return false;
else
{
// when you run this line the hibernate framework will try to retrieve the application data.Since you have the closed session lazy load exception occurs
user.getListOfApplications().add(app);
return true;
}

解决这个问题的方法

1) 尝试保持 session 打开,以便框架可以获取应用程序数据

2)在模型 pojo 类中将延迟加载更改为预先加载(因为您使用的是多对多关系,不建议使用这种方式)

关于java - 如何修复Java中的LazyInitializationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55368583/

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