0

JpaUtil commit WITHOUT closing the window

asked 2011-06-13 10:26:24 +0800

acirasa gravatar image acirasa
48 1

Hello Forum

I'have a zul view with a window. In this window there are two button: save and cancel

When I click on save I want to commit the transaction
When I click on cancel I want to rollback the transaction

I use JpaUtil so I haven't access to the transaction directly

How can I save all data in the view WITHOUT close the view?
How can I cancel all modified (rollback) data in the view WHEN I close the Window?

Thank you
My version of ZK i 5.0.5

delete flag offensive retag edit

11 Replies

Sort by ยป oldest newest

answered 2011-06-13 11:07:20 +0800

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

updated 2011-06-13 11:15:26 +0800

????????

When I click on cancel I want to rollback the transaction

Why not by clicking on cancel go back to an initial state of the Bean or View ??????



I'm working in this case: The View represents the data. The data comes from a bean or a list of beans (listbox).
I begin with a defined initial state of the bean/data/View

Before i do something like 'new' or 'edit' , i STORE the beans original data to a originalDataBean.
If i now need a cancel, than i copied back the original bean data.

In fact:
YOU CANNOT make a comitted transaction turn back with 'Rollback'.
To make a 'Rollback' should be the result of a failed commit.

How can I save all data in the view WITHOUT close the view?

????? There's no cause to close a view. It's on your hand how to implement the logic/code for saving the data.


best
Stephan

PS: Have a look on this online demo. There are several different working CRUD (Create, Read, Update, Delete )views.
Play with them to show the cancel effect which have nothing todo with 'Rollback'. Only read back the original data (before an action).

link publish delete flag offensive edit

answered 2011-06-13 11:33:30 +0800

acirasa gravatar image acirasa
48 1

updated 2011-06-13 11:49:52 +0800

Thank you Terry for your answer

"There's no cause to close a view. It's on your hand how to implement the logic/code for saving the data."

If I undestand correctly how JPAUtil work, this is wrong.

The commit of your work on the view is done ONLY when you CLOSE the view!

If I call EntityManager commit method I have a "Transaction not active" Exception, so I don't save updates manualy

Is this correct?


"Why not by clicking on cancel go back to an initial state of the Bean or View ??????"

a backup copy of bean... How can you do that? Clonable Interface? refresh from db?

link publish delete flag offensive edit

answered 2011-06-13 11:56:29 +0800

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

updated 2011-06-13 11:59:16 +0800

Hmmmm, sorry i don't know about the JPAUtil. But there must be methods in JPA for saving that have nothing todo with a frontend technology.

For the backup of the bean, i clone the 'bean properties' with a methode from org.apache.commons.beanutils.BeanUtils

/**
	 * Saves the selected object's current properties. We can get them back if a
	 * modification is canceled.
	 * 
	 * @see doResetToInitValues()
	 */
	public void doStoreInitValues() {

		if (getSelectedArticle() != null) {

			try {
				setOriginalArticle((Article) FDBeanUtils.cloneBean(getSelectedArticle()));
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	/**
	 * Reset the selected object to its origin property values.
	 * 
	 * @see doStoreInitValues()
	 * 
	 */
	public void doResetToInitValues() {

		if (getOriginalArticle() != null) {

			try {
				setSelectedArticle((Article) FDBeanUtils.cloneBean(getOriginalArticle()));

				// TODO Bug in DataBinder??
				windowArticleMain.invalidate();

			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


.
import java.lang.reflect.InvocationTargetException;

/**
 * EN: Utility class for beans.<br>
 * DE: Utility Klasse zum Manipulieren von Beans.<br>
 * 
 * @author Stephan Gerth
 */
public class FDBeanUtils {

	/**
	 * EN: Clone a bean based on the available property getters and setters,
	 * even if the bean class itself does not implement Cloneable.<br>
	 * Used by closing a tab/dialog to compare the initialBean's properties with
	 * the currentBean's properties to make a decision if the changed values
	 * must be stored.<br>
	 * DE: Gibt ein geklontes Bean aufgrund seiner vorhandenen Getter/Setter
	 * Werte zurueck. <br>
	 * Wird beim schliessen eines Tabs/Dialogs benutzt um die OriginalBean mit
	 * der aktuellenBean zu vergleichen. Besteht ein Unterschied kann
	 * entschieden werden, ob abgespeichert werden muss.<br>
	 * 
	 * @param bean
	 * @return
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	public static Object cloneBean(Object bean) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
		return org.apache.commons.beanutils.BeanUtils.cloneBean(bean);
	}

}


best
Stephan

link publish delete flag offensive edit

answered 2011-06-13 12:09:53 +0800

acirasa gravatar image acirasa
48 1

updated 2011-06-13 12:11:33 +0800

thank you stephan

beanutils is very usefull!

"But there must be methods in JPA for saving that have nothing todo with a frontend technology."

I absolutly agree! but I'm not found a solution WITHOUT direct access to entitymanager transaction!

Best regards
Andrea

link publish delete flag offensive edit

answered 2011-06-13 16:43:34 +0800

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

Hi Andrea,

have you a link to the JPAUtil.java code?

thanks
Stephan

link publish delete flag offensive edit

answered 2011-06-14 09:18:48 +0800

acirasa gravatar image acirasa
48 1

Hi Stephan

http://www.jarvana.com/jarvana/view/org/zkoss/zk/zkplus/5.0.0.RC/zkplus-5.0.0.RC-sources.jar!/org/zkoss/zkplus/jpa/JpaUtil.java?format=ok

In sources of ZK you can found last version


Bye

link publish delete flag offensive edit

answered 2011-06-14 13:19:16 +0800

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

Please, can you show us pieces of the code where you do saving.

link publish delete flag offensive edit

answered 2011-06-15 12:01:45 +0800

acirasa gravatar image acirasa
48 1

updated 2011-06-15 12:41:36 +0800

Sure

@Override
	protected void save() throws DaoException {
		if (person.getId()==null){
			Seat seat = getSeat();
			seat=dao.merge(seat);
			person.setSeat(seat);
			dao.persist(person);
		}
		else
			dao.merge(person);
		this.close(); //remove this line == remove the commit 
	}

this is an example of a save operation in PersonComposer (inherithed from GenericForwardComposer) object
the Dao object is in this context a simple proxy of entitymanager generated from zk JpaUtil

in this method we can insert (if id==null) or Update a Person object. When we call close (a metod for close the window) JpaUtil commit the transaction.

But if I remove close() the persist is NOT committed and this is not a good idea...

The problem is : I have no choice! If I try to call commit() from entitymanager.getTransaction JpaUtil throw Exception "Transaction not active".

PS: we use org.zkoss.zkplus.jpa.OpenEntityManagerInViewListener

Best regards
Andrea

link publish delete flag offensive edit

answered 2011-06-15 14:36:59 +0800

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

updated 2011-06-15 14:46:30 +0800

Hmmm, i have read http://184-106-66-216.static.cloud-ips.com/wiki/Small_Talks/2007/November/Using_ZK_JpaUtil_-_Retrieve_EntityManager_and_EntityManagerFactory . But there are no information for me how the transaction works.

By the way it's not a good choice to mix such things in the frontend.

After reading this i think dao.merge();is not the right method for what you want. use dao.persist(person) ; for the update too ?

best
Stephan

Edit: here are informations to persist and merge too

link publish delete flag offensive edit

answered 2011-06-16 03:18:00 +0800

acirasa gravatar image acirasa
48 1

updated 2011-06-16 03:24:10 +0800

Hi Stephan

I simplify my code for create a readble example and I delete an important detail

@Override
	protected void save() throws DaoException {
		if (person.getId()==null){
			Seat seat = getSeat();
			seat=dao.merge(seat);
			person.setSeat(seat);
			dao.persist(person);
		}
		else
			person=dao.merge(person);
		this.close(); //remove this line == remove the commit 
	}

The row:

person=dao.merge(person);

is (if I correctly undestend difference between persist and merge entitymanager methods) the correctly choice for an update operation of a detached bean.

I think my problem is a simplicistic implementation of org.zkoss.zkplus.jpa.OpenEntityManagerInViewListener

This listener in practice do his work in two view lifecycle point: when it's initialized and when it's cleanup
In init it's call begin transaction, and in cleanup its call commit.

This is very poor implemetation of "Transaction in view" hibernate pattern!

I have removed this listener and JpaUtil from my project

I think it's better have the plain control of transaction execution.

Best regards
Andrea

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-13 10:26:24 +0800

Seen: 492 times

Last updated: Jun 16 '11

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