我已經在Stackoverflow上看到過這種問題,但沒有任何東西可以幫助我解決我的問題。Hibernate,Java:沒有會話或會話被關閉
我是Hibernate的新手,並且有一個在Java和MySQL中製作的項目,因此使用hibernate。我設法達到我的數據,修改它們,刪除它們,但我阻止一種方法,因爲我有一個例外,它到達..並看到我仍然不是所有線程我管理不刪除此錯誤:
這是我的錯誤:
org.hibernate.LazyInitializationException 墓:無法初始化懶洋洋角色的集合: DAO.User.files,沒有會話或會話關閉 org.hibernate.LazyInitializationException:失敗懶惰地初始化 角色集合:DAO.User.files,沒有會話或會話被關閉 在 org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358) 在 org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350) 在 org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection .java:343) at org.hibernate.collection.PersistentSet.add(PersistentSet.java:189) at DAO.UserDAO.removeGroupAddUserFiles(UserDAO.java:252)at javaapplication5.JavaApplication5.main(JavaApplication5.java:37 )
這裏是我的Java類:
public static void main(String[] args) {
GroupDAO grDAO = new GroupDAO();
List[] lists = grDAO.removeGroup(grDAO.getGroupById(1));
FileDAO fiDAO = new FileDAO();
fiDAO.removeGroupAddFiles(lists);
UserDAO usDAO = new UserDAO();
usDAO.removeGroupAddUserFiles(lists);
}
GroupDAO.java:
public List[] removeGroup(Group group) {
Set<File> setFiles = group.getFiles();
Set<User> setUsers = group.getUsers_1();
List[] lists = new List[2];
Iterator<File> itFiles = setFiles.iterator();
Iterator<User> itUsers = setUsers.iterator();
List<File> listFiles = new ArrayList<File>();
List<User> listUsers = new ArrayList<User>();
while (itFiles.hasNext()) {
listFiles.add(itFiles.next());
}
while (itUsers.hasNext()) {
listUsers.add(itUsers.next());
}
lists[0] = listUsers;
lists[1] = listFiles;
org.hibernate.Transaction tx = session.beginTransaction();
session.delete(group);
tx.commit();
return lists;
}
FileDAO.java:
public List[] removeGroupAddFiles(List[] lists) {
System.out.println("5 : " + session.isOpen());
System.out.println("6 : " + session.isOpen());
Iterator<File> itFile = lists[1].iterator();
System.out.println("7 : " + session.isOpen());
org.hibernate.Transaction tx = session.beginTransaction();
while (itFile.hasNext()) {
System.out.println("8 : " + session.isOpen());
Iterator<User> itUser = lists[0].iterator();
System.out.println("9 : " + session.isOpen());
File f = itFile.next();
while (itUser.hasNext()) {
System.out.println("10 : " + session.isOpen());
System.out.println("11 : " + session.isOpen());
User u = itUser.next();
System.out.println("12 : " + session.isOpen());
File fCopie = new File(u, f.getName() + "_" + u.getFirstName() + "_" + u.getLastName(), f.getExtension(), f.getSize(), f.getCreatedAt(), f.getUpdateAt());
System.out.println("13 : " + session.isOpen());
session.save(fCopie);
System.out.println("14 : " + session.isOpen());
}
}
tx.commit();
return lists;
}
UserDAO.java:
public List[] removeGroupAddUserFiles(List[] lists) {
System.out.println("15 : " + session.isOpen());
Iterator<User> itUser = lists[0].iterator();
System.out.println("16 : " + session.isOpen());
org.hibernate.Transaction tx = session.beginTransaction();
while (itUser.hasNext()) {
System.out.println("17 : " + session.isOpen());
Iterator<File> itFile = lists[1].iterator();
System.out.println("18 : " + session.isOpen());
User u = itUser.next();
while (itFile.hasNext()) {
System.out.println("19 : " + session.isOpen());
File f = itFile.next();
System.out.println("20 : " + session.isOpen());
try {
u.getFiles().add(f);
} catch (LazyInitializationException e) {
e.printStackTrace();
}
System.out.println("21 : " + session.isOpen());
}
try {
session.update(u);
} catch (ConstraintViolationException e) {
e.printStackTrace();
}
System.out.println("22 : " + session.isOpen());
}
tx.commit();
return lists;
}
我的代碼多餘的,我知道他(它),這是極端的儘量避免會議結束的問題。
會話關閉在管路:
U.getFiles()。新增(F);
而且HibernateUtil.java:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// load from different directory
SessionFactory sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
return sessionFactory;
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
我把session.isOpen的顯示()無處不在我的代碼以及與該錯誤的嘗試摔跤。這個程序繼續下去,並且向我展示了系統的真實性,無論是在例外之前還是之後!
你試過看是否'用戶U = itUser.next();'在removeGroupAddUserFiles方法是做'u.getFiles之前附着到會話()加(F);'? – Faton
不,我沒有試過,因爲我不知道怎麼看到:S ... – algelos