Differences
This shows you the differences between two versions of the page.
tutorial:lazy_loading [2020/04/21 11:38] |
tutorial:lazy_loading [2020/04/21 11:38] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ==== Let the problem ==== | ||
+ | <columns 100% 500px> | ||
+ | {{tutorial:diagram.png?500 |Diagram}} | ||
+ | <newcolumn left> | ||
+ | To explain how lazy loading work in Adichatz, have a look on this diagram: | ||
+ | * An **Adress** depends on one and only one **City**.\\ | ||
+ | * A **City** could have zero or several **Adresses**.\\ | ||
+ | * A **City** depends on one and only one **Country**. | ||
+ | |||
+ | <WRAP indinc> | ||
+ | POJOs corresponding to this schema can be consult [[tutorial:pojo_schema|here]]. | ||
+ | </WRAP> | ||
+ | </columns> | ||
+ | |||
+ | With Hibernate, we can retrieve adress number 1 by using: | ||
+ | <sxh java>Adress address = (Adress) entityManager.find(Adress.class,1);</sxh> | ||
+ | |||
+ | If I try something like that: | ||
+ | <sxh java>String cityName = address.getCity().getCity();</sxh> | ||
+ | I will obtain a LazyIntializationException. That is normal because the fetch points to FetchType.LAZY in the ManyToOne relationship, so the city field loaded by Hibernate is only a proxy. So you have to manage this issue. | ||
+ | |||
+ | ==== Adichatz provides a solution: ==== | ||
+ | * Copy <wrap adicode>CustomerDIGENERATED.axml</wrap> file to <wrap adicode>CustomerDI.axml</wrap> which will become the reference for generated code. | ||
+ | * In the <wrap adicode>city</wrap> field replace <wrap adicode>convertModelToTarget</wrap> element as following: | ||
+ | <sxh xml> | ||
+ | <refText property="city" style="SWT.BORDER | AdiSWT.FIND_BUTTON | AdiSWT.DELETE_BUTTON" id="city"> | ||
+ | <convertModelToTarget>return "City:" + #FV().city + " - Country:" + #FV().Country.country;</convertModelToTarget> | ||
+ | </refText> | ||
+ | </sxh> | ||
+ | |||
+ | The displayed value would be in the generated **JAVA** class: | ||
+ | <sxh java> | ||
+ | return "City:" + ((Address) value).getCity().getCity() + " - Country:" + ((Address) value).getCity().getCountry().getCountry(); | ||
+ | </sxh> | ||
+ | |||
+ | <WRAP adihi><html><center><big></html>No exception occurs!<html></big></center></html></WRAP>\\ | ||
+ | ==== How it works ==== | ||
+ | The bean **adress** is wrapped into an **Entity** which is managed by the [[features:data_cache|Application Data Cache]]. | ||
+ | Adichatz associates a "**decision tree**" with each Entity. In this case the decision tree will be like this: | ||
+ | **Entity Adress** number **1** | ||
+ | * + city | ||
+ | * - city | ||
+ | * + Country | ||
+ | * - country | ||
+ | |||
+ | If in another editor, you want to display the number of **adresses** for the **city**, the decision tree becomes: | ||
+ | **Entity Adress** number **1** | ||
+ | * + city | ||
+ | * - name | ||
+ | * - adresses | ||
+ | * + Country | ||
+ | * - country | ||
+ | |||
+ | <WRAP indic> | ||
+ | In order to optimize calls to the server before displaying a page, Adichatz lists all the needed values (**lazy loadings**). If at least one is not initialized, it ask to reinitialize the decision tree to the server for all the entities having a missing value.\\ | ||
+ | This feature is entirely compliant with databinding process and ensures consistency between different editors. | ||
+ | </WRAP> | ||
+ | |||
+ |