0

StackOverflow when proxy object being submitted to origin

asked 2021-03-04 17:57:55 +0800

pavelEW gravatar image pavelEW
55 4

Hello, I have a bidirectional association in my model: Product OneToMany Price Price OneToOne Product

When I try to add a new price on a form and then save it I get a StackOverflow error, here is stacktrace from my project:

at my.model.Ware_$$_jvstd2_b5.submitToOrigin(Ware_$$_jvstd2_b5.java) 
at org.zkoss.bind.proxy.BeanProxyHandler.invoke(BeanProxyHandler.java:126) 
at my.model.Price_$$_jvstd2_b9.submitToOrigin(Price_$$_jvstd2_b9.java) 
at org.zkoss.bind.proxy.AbstractCollectionProxy.submitToOrigin(AbstractCollectionProxy.java:233) 
at org.zkoss.bind.proxy.BeanProxyHandler.invoke(BeanProxyHandler.java:126) 
at org.zkoss.bind.proxy.FormProxyHandler.invoke(FormProxyHandler.java:163) 
at my.model.Ware_$$_jvstd2_b5.submitToOrigin(Ware_$$_jvstd2_b5.java) 
at org.zkoss.bind.proxy.BeanProxyHandler.invoke(BeanProxyHandler.java:126) 
at my.model.Price_$$_jvstd2_b9.submitToOrigin(Price_$$_jvstd2_b9.java)
at org.zkoss.bind.proxy.AbstractCollectionProxy.submitToOrigin(AbstractCollectionProxy.java:233)

Here is a quick fiddle to reproduce this error: https://zkfiddle.org/sample/h7t36l/1-Another-new-ZK-fiddle

Press "Add price" button and then "Save".

Some explanations: In class Logic.java on 25 and 26 strings you'll find

price = product.getPrices().stream().findFirst().get();
price.setProduct(product);

I do this to get a proxied instance of Price, if I keep using an original object, then I get a Hibernate error, that my instance is transient and can't be saved.

In debug mode I see that after submitting all values to original price still contains proxied object of Ware (Product).

If I put an original ware object to original price object, then I get another one proxy instance of ware in price.

Any ideas?

delete flag offensive retag edit

1 Answer

Sort by » oldest newest most voted
0

answered 2021-03-05 16:54:58 +0800

MDuchemin gravatar image MDuchemin
2560 1 6
ZK Team

updated 2021-03-05 16:55:41 +0800

Hi there.

This structure will generate an infinite loop of cascading proxy objects. A product has a collection of prices, which in turn refer to the product, which refer to the price, etc.

You have to choose a point at which to use the actual object instead of a proxy. For example, you can decide that the proxy object targets "real" price objects instead of proxy prices. You can use the @immutable and @immutableelements annotations to do so.

http://books.zkoss.org/zk-mvvm-book/8.0/syntax/immutable.html

http://books.zkoss.org/zk-mvvm-book/8.0/syntax/immutableelements.html

See an example in fiddle. https://zkfiddle.org/sample/h7t36l/10-Another-new-ZK-fiddle

link publish delete flag offensive edit

Comments

Hi MDuchemin, I understand why this happens. The problem is that I have Maven multimodule with my entities in backend and ZK in front. Also, I guess that price.getProduct() will also return a non-proxied object which will show no changes before saving. In that case it breaks the logic

pavelEW ( 2021-03-07 04:16:14 +0800 )edit

Depends what the user is able to update on this screen. If they can add and remove entries to the price list for example, but not change the price properties, you don't need to proxy the price.

MDuchemin ( 2021-03-08 18:59:19 +0800 )edit

Users can do anything they want with prices. Add, remove, edit any property of the price. Some chages in product can also result into price recalculation.

pavelEW ( 2021-03-09 04:24:50 +0800 )edit

Assuming that the reference to the product in the price class is just a convenience link back to the holder, I'd say it probably doesn't need to be proxied.

MDuchemin ( 2021-03-09 11:14:17 +0800 )edit

do you have any structure in your view such as @load(product.prices[0].product) ?

MDuchemin ( 2021-03-09 11:14:51 +0800 )edit
Your answer
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
1 follower

RSS

Stats

Asked: 2021-03-04 17:57:55 +0800

Seen: 9 times

Last updated: Mar 05 '21

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