gpt4 book ai didi

java - 非法尝试将代理与两个打开的 session 相关联

转载 作者:行者123 更新时间:2023-11-29 05:02:37 24 4
gpt4 key购买 nike

如何修复此错误以及在 hibernate 中使用 session 的最佳方式是所有应用程序中的一个 session 和多个事务或者我所做的很好请检查下一个代码

我希望你用正确的代码给我答案,如果最好的话,如何在一个 session 中完成

这是错误

Exception in thread "AWT-EventQueue-0" org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions
at org.hibernate.proxy.AbstractLazyInitializer.setSession(AbstractLazyInitializer.java:112)
at org.hibernate.engine.internal.StatefulPersistenceContext.reassociateProxy(StatefulPersistenceContext.java:595)
at org.hibernate.engine.internal.StatefulPersistenceContext.unproxyAndReassociate(StatefulPersistenceContext.java:642)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:90)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)

hibernate 事务和实现

Session session = HibernateUtil.getSessionFactory().openSession();

Query query = null;
Transaction transaction = null;

@Override
public void delete(Car o) {
transaction = session.beginTransaction();
session.delete(o);
transaction.commit();

}

@Override
public List<Car> findAll() {
List<Car> carsList = new ArrayList<Car>();
try {
query = session.createQuery("from Car");
carsList = query.list();
for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) {
Car car = (Car) iterator.next();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {

}
return carsList;
}

@Override
public Car findById(int id) {
query = session.createQuery("from Car where id=:id");
query.setParameter("id", id);
Car car = (Car) query.list().get(0);
return car;
}

@Override
public void insert(Car o) {
transaction = session.beginTransaction();
session.save(o);
transaction.commit();
}

@Override
public void update(Car o) {
transaction = session.beginTransaction();
session.update(o);
transaction.commit();
}

@Override
public List<Car> findByCarLicense(CarLicense carLicense) {
List<Car> carsList = new ArrayList<Car>();
try {
query = session.createQuery("from Car where carLicense=:carLicense");
query.setParameter("carLicense", carLicense);
carsList = query.list();
for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) {
Car car = (Car) iterator.next();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {

}
return carsList;
}

@Override
public List<Car> findByPruCompany(PruCompany pruCompany) {
List<Car> carsList = new ArrayList<Car>();
try {
query = session.createQuery("from Car where pruCompany=:pruCompany");
query.setParameter("pruCompany", pruCompany);
carsList = query.list();
for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) {
Car car = (Car) iterator.next();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {

}
return carsList;
}

@Override
public List<Car> findByTypy(String type) {
List<Car> carsList = new ArrayList<Car>();
try {
query = session.createQuery("from Car where type=:type");
query.setParameter("type", type);
carsList = query.list();
for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) {
Car car = (Car) iterator.next();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {

}
return carsList;
}

@Override
public List<Car> findByModel(String model) {
List<Car> carsList = new ArrayList<Car>();
try {
query = session.createQuery("from Car where model=:model");
query.setParameter("model", model);
carsList = query.list();
for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) {
Car car = (Car) iterator.next();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {

}
return carsList;
}

@Override
public List<Car> findByColor(String color) {
List<Car> carsList = new ArrayList<Car>();
try {
query = session.createQuery("from Car where color=:color");
query.setParameter("color", color);
carsList = query.list();
for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) {
Car car = (Car) iterator.next();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {

}
return carsList;
}

@Override
public List<Car> findByNumber(String number) {
List<Car> carsList = new ArrayList<Car>();
try {
query = session.createQuery("from Car where number=:number");
query.setParameter("number", number);
carsList = query.list();
for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) {
Car car = (Car) iterator.next();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {

}
return carsList;
}

@Override
public List<Car> findByKilos(Double kilos) {
List<Car> carsList = new ArrayList<Car>();
try {
query = session.createQuery("from Car where kilos=:kilos");
query.setParameter("kilos", kilos);
carsList = query.list();
for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) {
Car car = (Car) iterator.next();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {

}
return carsList;
}

@Override
public List<Car> findByAvalibity(String avalibity) {
List<Car> carsList = new ArrayList<Car>();
try {
query = session.createQuery("from Car where avalibity=:avalibity");
query.setParameter("avalibity", avalibity);
carsList = query.list();
for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) {
Car car = (Car) iterator.next();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {

}
return carsList;
}

@Override
public Car findByChassisNumber(String chassisNumber) {
query = session.createQuery("from Car where chassisNumber=:chassisNumber");
query.setParameter("chassisNumber", chassisNumber);
Car car = (Car) query.list().get(0);
return car;
}

和 HibernateUtil

private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
SessionFactory sessionFactory = new Configuration().configure(
"/com/core/util/hibernate.cfg.xml").buildSessionFactory();
return sessionFactory;
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
getSessionFactory().close();
}

最佳答案

尽量不要为所有交易打开您的 session 对象,而是在每次使用后打开它并关闭它,这是从您写的内容中摘取的一个最小示例:

public void delete(Car o) {
transaction = session.beginTransaction();
session.delete(o);
transaction.commit();
}

public List<Car> findAll() {
List<Car> carsList = new ArrayList<Car>();
try {
query = session.createQuery("from Car");
carsList = query.list();
for (Iterator iterator = carsList.iterator(); iterator.hasNext(); ) {
Car car = (Car) iterator.next();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {

}
return carsList;
}

可能是这样的:

public class Transactor {

public static void delete(Car o) {
Session session;
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
} catch (HibernateException ex) {
session = HibernateUtil.getSessionFactory().openSession();
}
Transaction transaction = null;

try {
transaction = session.beginTransaction();
session.delete(o);
transaction.commit();
} catch (HibernateException ex) {
System.out.println("Error deleting car: " + ex);
if(transaction != null) {
transaction.rollback();
}
} finally {
if (session.isOpen()){
session.close();
}
}
}


public static List<Car> findAll() {
Session session;
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
} catch (HibernateException ex) {
session = HibernateUtil.getSessionFactory().openSession();
}
List<Car> carsList = new ArrayList<>();
try {
carsList = session.createQuery("from Car").list();
} catch (HibernateException he) {
System.out.println("Error getting cars: " + he);
he.printStackTrace();
} finally {
if (session.isOpen()){
session.close();
}
}
return carsList;
}
}

我不知道这是否是最好的方式,但更安全。

编辑

我已经编辑了例子,如果你想试试,你可以这样做:

List<Car> carList = Transactor.findAll(); // get all cars
Transactor.delete(myCar); // delete an specific car

从这里开始,您应该能够完成类(class)。

关于java - 非法尝试将代理与两个打开的 session 相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31551288/

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