Mhhh weiss ich auch nicht genau ... bin noch Anfänger was Hibernate angeht.
Ich Poste mal was von Sourcecode vielleicht kann mir einer sagen was ich mit der Session anders machen muss.
Das ist die Function die abspeichern versucht.
Code:
public String doActionSaveTranslation() {
FacesContext facesContext = FacesContext.getCurrentInstance();
UIForm rootForm = (UIForm) facesContext.getViewRoot().getChildren().get(0);
HtmlPanelGrid panelGrid = null;
Iterator it = rootForm.getChildren().iterator();
if (it != null) {
while (it.hasNext()) {
Object x = it.next();
if (x instanceof HtmlPanelGrid) {
panelGrid = (HtmlPanelGrid) x;
break;
}
}
List panelGridChildren = panelGrid.getChildren();
Iterator panelGridIter = panelGridChildren.iterator();
while (panelGridIter.hasNext()) {
Object object = panelGridIter.next();
if (object instanceof HtmlInputTextarea) {
HtmlInputTextarea htmlTextArea = (HtmlInputTextarea) object;
String id = htmlTextArea.getId();
if (id != null) {
if (id.startsWith("doc")) {
String tag = id.substring(3, id.length());
Iterator docTextIter = getEditDocument().getText().
iterator();
if (docTextIter != null) {
Boolean foundTag = false;
while (docTextIter.hasNext()) {
DocumentText docText = (DocumentText)docTextIter.next();
if (docText != null) {
if (tag.equals(docText.getTag())) {
String value = htmlTextArea.getValue().toString();
docText.setText(value);
qm.updateOrSave(docText);
foundTag = true;
}
}
}
if (!foundTag) {
DocumentText newDocText = new DocumentText();
newDocText.setText(htmlTextArea.getValue().toString());
newDocText.setTag(tag);
newDocText.setIsComplete(0);
newDocText.setDocument_ID(getEditDocument().getId());
qm.updateOrSave(newDocText);
}
}
}
}
}
}
}
return "doSave";
}
Die Funktion holt das Document mti denn DocumentText als Set
Code:
public String getDocumentName() {
System.out.println(" ##### getDocumentName()");
Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
int docId = (Integer)session.get("selectedDocID");
Documents document=null;
try {
document = qm.findDocumentByID(docId);
} catch (Exception ex) {
ex.printStackTrace();
}
if(document !=null) {
System.out.println(" setEditDocument(" + document + ")");
setEditDocument(document);
documentName = document.getDocName();
}else
return "Error";
pdfPreviewName= documentName;
return documentName;
}
Das sind die Hibernate querys in der Klasse QueryManager:
Code:
public Documents findDocumentByID(int id) throws Exception {
Session session = null;
Transaction tx = null;
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Iterator result=null;
try{
Query q = session.createQuery("FROM " + Documents.class.getName() +" as d WHERE d.id = :id");
q.setInteger("id",id);
result = q.iterate();
tx.commit();
}catch(Exception e) {
e.printStackTrace();
} finally {
if (result.hasNext()) {
Documents doc = (Documents) result.next();
return doc;
} else {
throw new Exception("Documents can't be find");
}
}
}
Das ist die Funktion die die Objecte abspeichert:
Code:
public void updateOrSave(Object document) {
Session session = null;
Transaction tx = null;
try {
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
session.saveOrUpdate(document);
tx.commit();
} catch (HibernateException ex) {
session.close();
if (tx != null)
try {
tx.rollback();
} catch (HibernateException exRb) {}
throw new RuntimeException(ex.getMessage());
}
}
Und das ist der SessionHandler
Code:
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static final Configuration cfg = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session != null && ! session.isConnected())
session = null;
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
// System.out.println(" ### Aktuelle SESSION: "+session);
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
private HibernateSessionFactory() {
}
}
Ich hoffe ihr könnt mir helfen ich verzweifle schon