0

[HELP] Problem with Hibernate, MySQL, ZK (CRUD Operation)

asked 2011-06-23 08:52:16 +0800

sidqy89 gravatar image sidqy89
21

updated 2011-06-23 08:52:50 +0800

hi there..
I'm new comer for zk..
i've tried to make an application based on zk-todo, but i try to use hibernate for crud operation, i have problem when try to run it

View index.zul

<?page title="Auto Generated index.zul"?>

<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>
<window id="win" title="To Do List" border="normal" width="640px"
	apply="org.todo.action.TodoAct">
	<listbox id="box" rows="5" model="@{win$composer.allEvents}"
	selectedItem="@{win$composer.current }">
		<listhead>
			<listheader label="Item" sort="auto(name)" />
			<listheader label="Priority" width="80px" sort="auto(priority)" />
			<listheader label="Date" width="170px" sort="auto(date)" />
		</listhead>
		<listitem self="@{each='event'}" value="@{event}">
			<listcell label="@{event.name }"/>
			<listcell label="@{event.priority }"/>
			<listcell label="@{event.date }"/>
		</listitem>
	</listbox>
	<groupbox>
	<caption label="Event">
	Item:	<textbox id="name" cols="25" value="@{win$composer.current.name}"/>
	Priority:	<textbox id="priority" cols="1" value="@{win$composer.current.priority}" />
	Date:	<datebox cols="8" value="@{win$composer.current.date}"/>
	<button id="add" onClick="" label="Add"></button>
	<button id="update" onClick="" label="Update"></button>
	<button id="delete" onClick="" label="Delete"></button>
	</caption>
	</groupbox>
</window>

controller TodoAct.java

package org.todo.action;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.omg.CORBA.Current;
import org.todo.model.TodoModel;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zkplus.hibernate.HibernateUtil;
import org.zkoss.zul.Button;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.SimpleListModel;
import org.zkoss.zul.Textbox;

public class TodoAct extends GenericForwardComposer {
	private static final long serialVersionUID = -9145887024839938515L;
	List<TodoModel> allMain = new ArrayList<TodoModel>();

	/* component */
	TodoModel current;
	Listbox box;
	Textbox name, priority;
	Datebox date;
	Button add, update, delete;

	AnnotateDataBinder binder;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);

		binder = new AnnotateDataBinder(comp);
		Session em = HibernateUtil.currentSession();
		System.out.print("doAfterCompose");
	}

	public void onClick$add() throws InterruptedException {
		Session session = HibernateUtil.currentSession();
		current.setName(name.getValue());
		current.setPriority(priority.getValue());
		current.setDate(date.getValue());
		session.saveOrUpdate(current);
	}

	public void onClick$update() throws InterruptedException {
		name.setValue(current.getName());
		priority.setValue(current.getPriority());
		date.setValue(current.getDate());
	}

	public List<TodoModel> getAllMain() {
		Session session = HibernateUtil.currentSession();
		allMain = session.createCriteria(TodoModel.class).list();
		return allMain;
	}

	public void setAllMain(List<TodoModel> allMain) {
		this.allMain = allMain;
	}

	public void renderList() {
		SimpleListModel model = new SimpleListModel(getAllMain());
		box.setModel(model);
		box.setItemRenderer(new ListitemRenderer() {

			@Override
			public void render(Listitem item, Object data) throws Exception {
				// TODO Auto-generated method stub
				final TodoModel todo = (TodoModel) data;

				new Listcell("" + todo.getName()).setParent(item);
				new Listcell("" + todo.getPriority()).setParent(item);
				new Listcell("" + todo.getDate()).setParent(item);
				item.addEventListener("OnClick", new EventListener() {

					@Override
					public void onEvent(Event event) throws Exception {
						// TODO Auto-generated method stub
						current = todo;
					}
				});
			}
		});
	}
}

Model TodoModel.java

package org.todo.model;

import java.util.Date;

import javax.persistence.*;

@Entity
public class TodoModel {
	private String id;
	private String name;
	private String priority;
	private Date date;
	
	@Id
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPriority() {
		return priority;
	}
	public void setPriority(String priority) {
		this.priority = priority;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	
}

and this show when I run the app

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.zkoss.zk.ui.UiException: Expects java.util.Set, java.util.List, java.util.Map, Object[], Enum Class, or BindingListModel only. class org.todo.action.TodoAct
	org.zkoss.zkplus.databind.ListModelConverter.coerceToUi(ListModelConverter.java:57)
	org.zkoss.zkplus.databind.Binding.myLoadAttribute(Binding.java:366)
	org.zkoss.zkplus.databind.Binding.loadAttribute(Binding.java:336)
	org.zkoss.zkplus.databind.DataBinder.loadAttrs(DataBinder.java:591)
	org.zkoss.zkplus.databind.DataBinder.loadComponent0(DataBinder.java:546)
	org.zkoss.zkplus.databind.DataBinder.loadAll(DataBinder.java:575)
	org.zkoss.zkplus.databind.AnnotateDataBinderInit.doAfterCompose(AnnotateDataBinderInit.java:175)
	org.zkoss.zk.ui.impl.RealInits.doAfterCompose(Initiators.java:82)
	org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:376)
	org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:291)
	org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:243)
	org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:164)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.

could you tell me where i have to fix..

best regards!

delete flag offensive retag edit

4 Replies

Sort by » oldest newest

answered 2011-06-23 21:01:08 +0800

sidqy89 gravatar image sidqy89
21

anybody can help me??

link publish delete flag offensive edit

answered 2011-06-24 00:15:23 +0800

kevin207 gravatar image kevin207
9

Hi Sidqy,

The trouble’s cause ZK framework together with Hibernate you might run into some issues where Hibernate behaves a little funky while the application runs. ZK’s multi-threaded event model. Whenever an event gets fired, a new event thread is created. This will lead to multiple instances of the Hibernate session class (one per thread) and thus lead to unpredictable behavior.


Regards
Kevin Thomas
Attune Infocom Inc
Email : [email protected]
Liferay Portlet

link publish delete flag offensive edit

answered 2011-06-24 03:28:12 +0800

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

updated 2011-06-24 03:29:13 +0800

you have defined the model in your zul:

 model="@{win$composer.allEvents}"


I cannot find the getter for this model.

BindingListModelList allEvents;

   public BindingListModelList getAllEvents() {
      return allEvents;
   }

   public void setAllEvents(BindingListModelList allEvents) {
      this.allEvents = allEvents;
   }

next you must fill the ListModel at the right place in your code

setAllEvents(new BindingListModelList( getAllMain()));

public List<TodoModel> getAllMain() {
		Session session = HibernateUtil.currentSession();
		allMain = session.createCriteria(TodoModel.class).list();
		return allMain;
	}


best
Stephan

link publish delete flag offensive edit

answered 2011-07-28 21:52:42 +0800

sidqy89 gravatar image sidqy89
21

thx for the reply, i've solved the problem :)

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: 2011-06-23 08:52:16 +0800

Seen: 972 times

Last updated: Jul 28 '11

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