-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4506277
By: onlytre
Im new to zk , im trying to use databinder with hibernate (NO SPRING), i followed the databind4 example , i configured zk.xml adding the threadlocal configuration suggested in zk 2.4.1 release notes:
<listener>
<description>ThreadLocal Variables Synchronizer</description> <listener-class>org.zkoss.zkplus.util.ThreadLocalListener</listener-class>
</listener>
<preference>
<name>ThreadLocal</name>
<value>
org.hibernate.context.ThreadLocalSessionContext=context;
</value>
</preference>
but when i change the value in a textbox this change is not reflected on the DB, but only on the interface.
this is my .zul
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?> <window title="demo Indicatori" id="win"
xmlns:a="http://www.zkoss.org/2005/zk/annotation" width="800px"
use="handlers.IndicatoriWindow" >
<a:bind model="win.indicatori" selectedItem="win.selected"/>
<listbox id="indicLb" mold="paging">
<listhead>
<listheader label="Indicatore" width="100px"/>
<listheader label="Descrizione" width="100px"/>
<listheader label="Data" width="100px"/>
</listhead>
<a:bind _var="indicatore"/>
<listitem >
<a:bind label="indicatore.nome"/>
<listcell draggable="true"/>
<a:bind label="indicatore.data"/>
<listcell/>
<a:bind label="indicatore.schedaIndicatore.data"/>
<listcell/>
</listitem>
</listbox>
<div align="center">
<hbox>
<button label="add" onClick="win.onAdd()" />
</hbox>
</div>
<!-- show the detail of the selected person -->
<tabbox width="700px" >
<tabs>
<tab id="tab1" label="Informazioni Base" />
<tab id="tab2" label="Informazioni Avanzate" />
</tabs>
<tabpanels>
<tabpanel>
<tabbox orient="vertical">
<tabs>
<tab label="A"/>
<tab label="B"/>
</tabs>
<tabpanels width="600px">
<tabpanel id="tabpanel11">
<grid width="600px">
<columns>
<column />
<column />
</columns>
<rows>
<row>
<label value="nome* " />
<textbox id="nome" />
</row>
<row>
<label value="descrizione " />
<textbox />
</row>
<row>
<label value="codice sipa" />
<a:bind value="win.selected.schedaIndicatore.codiceSipa;
save-when:self.onChange" /><textbox />
</row>
<row>
<label value="anno*" />
<a:bind value="win.selected.schedaIndicatore.data"/><textbox />
</row>
</rows>
</grid>
this is my hibernate session factory class:
package hibDAO;
import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
this is my Indicatore class:
public class Indicatore implements Serializable {
private String nome;
private Date data;
private Integer id; // @jve:decl-index=0:
private SchedaIndicatore schedaIndicatore; // @jve:decl-index=0:
private AreaTematica areaTematica;
private Set dettagliIndicatore = new LinkedHashSet();
public AreaTematica getAreaTematica() {
return areaTematica;
}
public void setAreaTematica(AreaTematica areaTematica) {
this.areaTematica = areaTematica;
}
/**
* @return the dettagliIndicatore
*/
public Set getDettagliIndicatore() {
return dettagliIndicatore;
}
/**
* @param dettagliIndicatore the dettagliIndicatore to set
*/
public void setDettagliIndicatore(Set dettagliIndicatore) {
this.dettagliIndicatore = dettagliIndicatore;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public SchedaIndicatore getSchedaIndicatore() {
return schedaIndicatore;
}
public void setSchedaIndicatore(SchedaIndicatore schedaIndicatore) {
this.schedaIndicatore = schedaIndicatore;
}
public Date getData() {
return data;
}
public void setData(Date data) {
this.data = data;
}
}
this is my IndicatoriDAO class
public class IndicatoreDAO {
public IndicatoreDAO()
{
_session = HibernateSessionFactory.getSession();
}
public void saveOrUpdate(Indicatore anIndicatore)
{
_session.SaveOrUpdate(anIndicatore);
}
public void saveOrUpdate(Indicatore anIndicatore,SchedaIndicatore
anScheda)
{
anIndicatore.setSchedaIndicatore(anScheda);
_session.saveOrUpdate(anIndicatore);
}
public void delete(Indicatore anIndicatore)
{
_session.delete(anIndicatore);
}
public Indicatore findById(Integer id)
{
return (Indicatore)_session.load(Indicatore.class, id);
}
public List findAll()
{
return _session.createQuery("from Indicatore").list();
}
Session _session;
}
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4508055
By: henrichen
Since you manage your own Hibernate Session in HibernateSessionFactory, you have to config zk.xml as follows (Per the code your provide):
<listener>
<description>ThreadLocal Variables Synchronizer</description> <listener-class>org.zkoss.zkplus.util.ThreadLocalListener</listener-class>
</listener>
<preference>
<name>ThreadLocal</name>
<value>
hibDAO.HibernateSessionFactory=threadLocal; <-- copy your ThreadLocal variables for you --> </value> </preference>
The original configuration is used with Hibernate's "thread" session context.
(hibernate.current_session_context_class = thread).
And I did not see how you open a hibernate session and close a session. So ...
Please see this article regarding why we have to copy ThreadLocal variables in ZK environment.
http://www.zkoss.org/smalltalks/hibernatezk/hibernatezk.dsp
Or, if you don't use modal window in your application, etc. you can in fact "turn" ZK into "thread-safe" mode (i.e. you don't have to worry about ThreadLocal issues). Just config following in zk.xml:
<system-config>
<disable-event-thread/>
</system-config>
/henri
Asked: 2007-09-07 10:39:01 +0800
Seen: 193 times
Last updated: Sep 08 '07