0

ZK + Hibernate + MySQL + ClassDAO

asked 2009-07-06 17:35:45 +0800

tinfinitus gravatar image tinfinitus
21 1

Hi!!I have a problem, I'm working with Hibernate, ZK and MySQL for the first time and i'm a little loss.I already create my source code Cargo.java, hibernate.cfg.xml, Cargo.hbm.xml and my Cargo.zul to make a simple test, but when run the .zul file i can't see my grid, just 4 images that looks like buttons, what am i doing wrong?Here is my code:

Cargo.java

package ClasesDominio;
public class Cargo {
 
	     private Double idCargo;
	     private String descripcion;
	     
	     public Cargo() {
		}
	     
		//SETTERS
         public void setIdCargo(Double idCargo) {
	         this.idCargo = idCargo;
	     }
         public void setDescripcion(String descripcion) {
	         this.descripcion = descripcion;
         }
	     //GETTERS
         public Double getIdCargo() {
    	             return idCargo;
    	     }
         public String getDescripcion() {
	         return descripcion;
	     }
}

CargoDAO.java

package ClasesDAO;
import java.util.List;
import org.hibernate.Session;
import org.zkoss.zkplus.hibernate.HibernateUtil;

public class CargoDAO {

    Session currentSession() {
        return HibernateUtil.currentSession();
    }
  
    public void guardar(ClasesDominio.Cargo unCargo, String descripcion) {
        Session sess =  currentSession();
        unCargo.setDescripcion(descripcion);
        sess.saveOrUpdate(unCargo);
    }
    public void eliminar(ClasesDominio.Cargo unCargo) {
        Session sess =  currentSession();
        sess.delete(unCargo);
    }
    public ClasesDominio.Cargo buscar(Double idCargo) {
        Session sess =  currentSession();
        return (ClasesDominio.Cargo) sess.load(ClasesDominio.Cargo.class, idCargo);
    }
    @SuppressWarnings("unchecked")
	public List buscarTodo() {
        Session sess =  currentSession();
        return sess.createQuery("from Cargo").list();
    }
}

Cargo.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
	
<hibernate-mapping>

  <class name="ClasesDominio.Cargo" table="mm_Cargo">
  	<id name="idCargo" column="PK_IdCargo">
  		<generator class="increment"/>
  	</id>
  		<property name="descripcion" type="String" column="descripcion" length="50"></property>  
  </class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
		
<hibernate-configuration>

    <session-factory>
    
     <!-- Configuración de conexión a la Base de Datos -->
        <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/PortalWeb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        
     <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
     <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
     <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>
     <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
		<property name="transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property> 
    	<property name="show_sql">true</property> 
	    <property name="use_outer_join">true</property>
	
     <!-- Mapping files --> 
		
		<mapping resource="Configuracion/Cargo.hbm.xml" />

    </session-factory>
</hibernate-configuration>

Cargo.zul

<?page title="Registro de Cargo" contentType="text/html;charset=UTF-8"?>
<zk>
<zscript><![CDATA[
import ClasesDAO.CargoDAO;
//fetch all allEvents from database
	List allCargos = new ClasesDAO.CargoDAO().buscarTodo();
]]></zscript>
<separator height="20px"/>
<grid width="600px" height="50px" visible="true" align="center" tooltiptext="Grid">
	<columns>
		<column width="100%" valign="top"/>
	</columns>
	<rows>
		<row>
			<hbox>
				<label value="Descripción " style="font-size:15px;font-weight:bold;color:darkblue;"/>
				<space width="25px"/><textbox id="txtDescripcion" width="450px"/>
			</hbox>
		</row>
	</rows>
</grid>
<space/>
<div align="center" width="600px" tooltiptext="Div">
	<button image="/Imagenes/grabar.png" id="btnGuardar" tooltiptext="Guardar"/>
	<button image="/Imagenes/eliminar.png" id="btnEliminar" tooltiptext="Eliminar"/>
	<button image="/Imagenes/cancelar.png" id="btnCancelar" tooltiptext="Cancelar"/>
	<button image="/Imagenes/salir.png" id="btnSalir" tooltiptext="Salir"/>
</div>
</zk>

I hope you can help me!Thanks!!!

delete flag offensive retag edit

5 Replies

Sort by » oldest newest

answered 2009-07-06 18:06:02 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2009-07-06 18:06:51 +0800

Hmmmmm,
i don't work with zscript. But i mean you need list/grid-model and an itemRenderer. If you can take a listbox look here for codes.

regards
Stephan

1. Perhaps this is older 3.5.2 code with Comparator.
2. I miss the equals and hashcode methods in your model class. Look in the model class 'Branch.java'

link publish delete flag offensive edit

answered 2009-07-06 18:07:14 +0800

YamilBracho gravatar image YamilBracho
1722 2

But I do not see in your zul the code to fill the grid using the "allCargos" variable...
Typically is a forEach attribute. Check http://docs.zkoss.org/wiki/Data_Binding_Collection_Data_with_ZUML_Annotations

link publish delete flag offensive edit

answered 2009-07-07 12:28:14 +0800

tinfinitus gravatar image tinfinitus
21 1

Sorry that code (allCargos) was a test but i don't need that function in this moment because at this moment I can not even see my .zul, just 4 buttons without label or textbox!i wanna see the .zul file firts!

link publish delete flag offensive edit

answered 2009-07-07 13:21:38 +0800

YamilBracho gravatar image YamilBracho
1722 2

I run your code and it works as expected. It shows me The "Description label" and one TextBox.
What ZK version are yo using ? I am using ZK 3.6.2

link publish delete flag offensive edit

answered 2009-07-07 14:44:23 +0800

tinfinitus gravatar image tinfinitus
21 1

Yeah, i think that my configuration files are wrong. I found a zk + hibernate tutorial that includes a complete example and run perfectly in my Zk. So, "import" my files in that example, change the package names, the project name, delete the example files and run my project...Perfect. Despite this i will continue looking for what is the configuration that i need. Thanks!

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-07-06 17:35:45 +0800

Seen: 1,501 times

Last updated: Jul 07 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More