如何修复此错误以及在hibernate中使用会话的最佳方法是在所有应用程序中进行一次会话和多次事务,或者我做的是好的请检查下一个代码
我希望你能用正确的代码给我回答,如果最好的话,如何在一个会话中使用
这是错误
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();
}
答案 0 :(得分:2)
尽量不要为所有交易打开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
从这里开始,你应该能够完成你的课程。