📄 xwikihibernatestore.java
字号:
if (bTransaction) { checkHibernate(context); bTransaction = beginTransaction(context); } Session session = getSession(context); session.load(property, (Serializable) property); // TODO: understand why collections are lazy loaded // Let's force reading lists if there is a list // This seems to be an issue since Hibernate 3.0 // Without this test ViewEditTest.testUpdateAdvanceObjectProp fails if (property instanceof ListProperty) { ((ListProperty)property).getList(); } if (bTransaction) { endTransaction(context, false); } } catch (Exception e) { BaseCollection obj = property.getObject(); Object[] args = { (obj!=null) ? obj.getName() : "unknown", property.getName() }; throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while saving property {1} of object {0}", e, args); } finally { try { if (bTransaction) endTransaction(context, false); } catch (Exception e) {} } } public void saveXWikiProperty(PropertyInterface property, XWikiContext context, boolean bTransaction) throws XWikiException { try { if (bTransaction) { checkHibernate(context); bTransaction = beginTransaction(context); } Session session = getSession(context);// I'm using a local transaction// There might be implications to this for a wider transaction Transaction ltransaction = session.beginTransaction();// Use to chose what to delete boolean isSave = false; try { Query query = session.createQuery("select prop.name from BaseProperty as prop where prop.id.id = :id and prop.id.name= :name"); query.setInteger("id", property.getId()); query.setString("name", property.getName()); if (query.uniqueResult()==null) { isSave = true; session.save(property); } else { isSave = false; session.update(property); } session.flush(); ltransaction.commit(); } catch (Exception e) {// We can't clean-up ListProperties if (property instanceof ListProperty) throw e;// This seems to have failed..// This is an attempt to cleanup a potential mess// This code is only called if the tables are in an incoherent state// (Example: data in xwikiproperties and no data in xwikiintegers or vice-versa)// TODO: verify of the code works with longer transactions BaseProperty prop2;// Depending on save/update there is too much data either// in the BaseProperty table or in the inheritated property table// We need to delete this data if (isSave) prop2 = (BaseProperty) property; else prop2 = new BaseProperty(); prop2.setName(property.getName()); prop2.setObject(property.getObject()); ltransaction.rollback();// We need to run the delete in a separate session// This is not a problem since this is cleaning up Session session2 = getSessionFactory().openSession(); Transaction transaction2 = session2.beginTransaction(); session2.delete(prop2); session2.flush();// I don't understand why I can't run this in the general session// This might make transactions fail if (!isSave) session2.save(property); transaction2.commit(); session2.close(); } if (bTransaction) endTransaction(context, true); } catch (Exception e) { BaseCollection obj = property.getObject(); Object[] args = { (obj!=null) ? obj.getName() : "unknown", property.getName() }; throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while saving property {1} of object {0}", e, args); } finally { try { if (bTransaction) endTransaction(context, false); } catch (Exception e) {} } } public void saveXWikiClass(BaseClass bclass, XWikiContext context, boolean bTransaction) throws XWikiException { try { if (bTransaction) { checkHibernate(context); bTransaction = beginTransaction(context); } Session session = getSession(context);// Verify if the property already exists Query query = session.createQuery("select obj.id from BaseClass as obj where obj.id = :id"); query.setInteger("id", bclass.getId()); if (query.uniqueResult()==null) session.save(bclass); else session.update(bclass); // Remove all existing properties if (bclass.getFieldsToRemove().size()>0) { for (int i=0;i<bclass.getFieldsToRemove().size();i++) { session.delete(bclass.getFieldsToRemove().get(i)); } bclass.setFieldsToRemove(new ArrayList()); } Collection coll = bclass.getFieldList(); Iterator it = coll.iterator(); while (it.hasNext()) { PropertyClass prop = (PropertyClass) it.next(); saveXWikiClassProperty(prop, context, false); } if (bTransaction) { endTransaction(context, true); } } catch (Exception e) { Object[] args = { bclass.getName() }; throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_CLASS, "Exception while saving class {0}", e, args); } finally { try { if (bTransaction) endTransaction(context, false); } catch (Exception e) {} } } public void loadXWikiClass(BaseClass bclass, XWikiContext context, boolean bTransaction) throws XWikiException { try { if (bTransaction) { checkHibernate(context); bTransaction = beginTransaction(context); } Session session = getSession(context); try { session.load(bclass, new Integer(bclass.getId())); } catch (ObjectNotFoundException e) {// There is no class data saved bclass = null; return; } HashMap map = new HashMap(); Query query = session.createQuery("select prop.name, prop.classType from PropertyClass as prop where prop.id.id = :id order by prop.number asc"); query.setInteger("id", bclass.getId()); Iterator it = query.list().iterator(); while (it.hasNext()) { Object obj = it.next(); Object[] result = (Object[]) obj; String name = (String)result[0]; String classType = (String)result[1]; PropertyClass property = (PropertyClass) Class.forName(classType).newInstance(); property.setName(name); property.setObject(bclass); session.load(property, property); bclass.addField(name, property); } if (bTransaction) { endTransaction(context, true); } } catch (Exception e) { Object[] args = { bclass.getName() }; throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_CLASS, "Exception while loading class {0}", e, args); } finally { try { if (bTransaction) endTransaction(context, false); } catch (Exception e) {} } } public void saveXWikiClassProperty(PropertyClass property, XWikiContext context, boolean bTransaction) throws XWikiException { try { if (bTransaction) { checkHibernate(context); bTransaction = beginTransaction(context); } Session session = getSession(context);// I'm using a local transaction// There might be implications to this for a wider transaction Transaction ltransaction = session.beginTransaction();// Use to chose what to delete boolean isSave = false; try { Query query = session.createQuery("select prop.name from PropertyClass as prop where prop.id.id = :id and prop.id.name= :name"); query.setInteger("id", property.getId()); query.setString("name", property.getName()); if (query.uniqueResult()==null) { isSave = true; session.save(property); } else { isSave = false; session.update(property); } session.flush(); ltransaction.commit(); } catch (Exception e) {// This seems to have failed..// This is an attempt to cleanup a potential mess// This code is only called if the tables are in an incoherent state// (Example: data in xwikiproperties and no data in xwikiintegers or vice-versa)// TODO: verify of the code works with longer transactions PropertyClass prop2;// Depending on save/update there is too much data either// in the BaseProperty table or in the inheritated property table// We need to delete this data if (isSave) prop2 = (PropertyClass) property; else prop2 = new PropertyClass(); prop2.setName(property.getName()); prop2.setObject(property.getObject()); ltransaction.rollback();// We need to run the delete in a separate session// This is not a problem since this is cleaning up Session session2 = getSessionFactory().openSession(); Transaction transaction2 = session2.beginTransaction(); session2.delete(prop2); session2.flush();// I don't understand why I can't run this in the general session// This might make transactions fail if (!isSave) session2.save(property); transaction2.commit(); session2.close(); } if (bTransaction) endTransaction(context, true); } catch (Exception e) { Object[] args = { property.getObject().getName() }; throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_CLASS, "Exception while saving class {0}", e, args); } finally { try { if (bTransaction) endTransaction(context, false); } catch (Exception e) {} } } public void loadAttachmentList(XWikiDocument doc, XWikiContext context, boolean bTransaction) throws XWikiException { try { if (bTransaction) { checkHibernate(context); bTransaction = beginTransaction(context); } Session session = getSession(context); Query query = session.createQuery("from XWikiAttachment as attach where attach.docId=:docid"); query.setLong("docid", doc.getId()); List list = query.list(); for (int i=0;i<list.size();i++) { ((XWikiAttachment)list.get(i)).setDoc(doc); } doc.setAttachmentList(list); if (bTransaction) endTransaction(context, false); } catch (Exception e) { e.printStackTrace(); Object[] args = { doc.getFullName() }; throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SEARCHING_ATTACHMENT, "Exception while searching attachments for documents {0}", e, args); } finally { try { if (bTransaction) endTransaction(context, false); } catch (Exception e) {} } } public void saveAttachmentList(XWikiDocument doc, XWikiContext context, boolean bTransaction) throws XWikiException { try { if (bTransaction) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -