Problem with JSON and hibernate lazy

223 views
Skip to first unread message

SJ.Jafari

unread,
Aug 14, 2010, 11:19:43 AM8/14/10
to struts2-jquery
I've got lots of chained objects stored in DB via hibernate, now when
I want to load those objects back into the grid I get tremendous sized
objects containing unnecessary data.
On the other hand JSON doesn't support hibernate lazy so I should load
the objects completely.
this isn't practical at all.

I defined some equivalent objects to only represent necessary data, it
works fine but funny thing I can't select any rows in the grid in this
case! when I select any row in the grid the first grid gets selected!
what kinda problem is that?!

btw is there any way to send XML data to the grid?

Yael Joyas

unread,
Aug 14, 2010, 11:58:33 AM8/14/10
to struts2-jquery
how many grids do you have on your page?
what version of the plugin and struts are you using?
do you have code sample?

SJ.Jafari

unread,
Aug 15, 2010, 4:54:30 AM8/15/10
to struts2-jquery
In some of them just one grid and in the other more than three, the
problem stands in all of them.

I use these libreries:
struts2-core-2.1.8.1.jar
struts2-jquery-grid-plugin-2.3.1.jar
struts2-jquery-plugin-2.3.1.jar

here is the action code that loads heavy objects with setting the
hibernate lazy to false and in the result page I'm able to select
rows:

public String execute() {
Player player = null;
try {
Map session = (Map) ActionContext.getContext().get("session");
String UID = (String) session.get("UID");
player = UserSession.getPlayerById(UID);

// Count Rows
records = HPOSG.getStocksList().size();
rows = records;

// Load Stocks from DB via hibernate
gridModel = (List<Stock>) HPOSG.getStocksList();

// calculate the total pages for the query
total = (int) Math.ceil((double) records / (double) rows);
System.out.println("I'm a JSON Action ");

} catch (NoSuchPlayer e) {
e.printStackTrace();
} catch (Exception e) {
addActionError(e.getMessage());
return "error";
}

return SUCCESS;
}

public String getJSON() {
return execute();
}

to avoid loading these heavy objects into the grid I planned to define
some representation Objects containing only necessary data:

public String execute() {
Player player = null;
try {
Map session = (Map) ActionContext.getContext().get("session");
String UID = (String) session.get("UID");
player = UserSession.getPlayerById(UID);

// Count Rows
records = HPOSG.getStocksList().size();
rows = records;


gridModel.removeAll(gridModel);
// ADD DATA TO REPRESANTATION OBJECTS

for (Stock db_Stock : HPOSG.getStocksList()) {
Stock_Rep stockForRepresent = new Stock_Rep();

stockForRepresent.setBestOffer(db_Stock.getBestOffer());
stockForRepresent.setBestRequest(db_Stock.getBestRequest());
stockForRepresent.setChanged(db_Stock.getChanged());
stockForRepresent.setCorpName(db_Stock.getCorpName());
stockForRepresent.setFinishedPrice(db_Stock.getFinishedPrice());
stockForRepresent.setLastPrice(db_Stock.getLastPrice());
stockForRepresent.setMaxPrice(db_Stock.getMaxPrice());
stockForRepresent.setMinPrice(db_Stock.getMinPrice());
stockForRepresent.setPercent(db_Stock.getPercent());
stockForRepresent.setPreviousPrice(db_Stock.getPreviousPrice());
stockForRepresent.setSymbol(db_Stock.getSymbol());
stockForRepresent.setYesterdayPrice(db_Stock
.getYesterdayPrice());
gridModel.add(stockForRepresent);
}

// calculate the total pages for the query
total = (int) Math.ceil((double) records / (double) rows);
System.out.println("I'm a JSON Action ");

} catch (NoSuchPlayer e) {
e.printStackTrace();
} catch (Exception e) {
addActionError(e.getMessage());
return "error";
}

return SUCCESS;
}

public String getJSON() {
return execute();
}

In this scenario the data is being loaded into the grid successfully
but surprisingly I can't select the rows!
I really don't understand what's going on here :(

SJ.Jafari

unread,
Aug 15, 2010, 5:09:54 AM8/15/10
to struts2-jquery
here is the grid tag:
<!-- ---BuyGrid--- -->

<s:url id="remoteurl" action="boardLoadBuy" />
<sjg:grid
id="grid_buy" filter="true" filterOptions="{stringResult:
true,searchOnEnter : false}"
caption="تابلوی بازار بورس" dataType="json" href="%{remoteurl}"
gridModel="gridModel" rowNum="20000" height="165"
onSelectRowTopics="rowSelect"
>

<sjg:gridColumn name="bestOffer" title="بهترین عرضه" >
</sjg:gridColumn>
<sjg:gridColumn name="bestRequest" title="بهترین تقاضا" >
</sjg:gridColumn>
<sjg:gridColumn name="percent" title="درصد">
</sjg:gridColumn>
<sjg:gridColumn name="changed" title="تغییر">
</sjg:gridColumn>
<sjg:gridColumn name="yesterdayPrice" title="قیمت دیروز">
</sjg:gridColumn>
<sjg:gridColumn name="finishedPrice" title="قیمت پایانی" >
</sjg:gridColumn>
<sjg:gridColumn name="lastPrice" title="آخرین قیمت" >
</sjg:gridColumn>
<sjg:gridColumn name="minPrice" title="کمترین قیمت" >
</sjg:gridColumn>
<sjg:gridColumn name="maxPrice" title="بیشترین قیمت" >
</sjg:gridColumn>
<sjg:gridColumn name="corpName" title="نام شرکت" >
</sjg:gridColumn>
<sjg:gridColumn name="symbol" title="نماد" align="right" width="55">
</sjg:gridColumn>
</sjg:grid>

And struts config:
<action name="boardLoadBuy"
class="com.hposg.controller.struts.BoardLoadActionBuy" >
<result type="json" name="success"></result>
<result type="json" name="error"></result>
</action>

jogep

unread,
Aug 16, 2010, 7:51:08 AM8/16/10
to struts2-jquery
i think the problem is that you don't have defined a key column.
add a column with an unique id and set attribute key="true".

Johannes
---
web: http://www.jgeppert.com
twitter: http://twitter.com/jogep

José Yoshiriro

unread,
Aug 16, 2010, 8:02:44 AM8/16/10
to struts2...@googlegroups.com
I solved this using "excludeProperties" or "includeProperties" parameters:
http://struts.apache.org/2.2.1/docs/json-plugin.html


2010/8/16 jogep <joh...@googlemail.com>
--
You received this message because you are subscribed to the Google Groups "struts2-jquery" group.
To post to this group, send email to struts2...@googlegroups.com.
To unsubscribe from this group, send email to struts2-jquer...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/struts2-jquery?hl=en.




--
Atenciosamente,

José Yoshiriro Ajisaka Ramos
Sun Certified Professional (SCJP 6, SCWCD 5, SCBCD 5) / Articulista MundoJ (antiga "MundoJava") / Colaborador da Apache.org
http://jyoshiriro.blogspot.com/
 
"Onde houver dúvidas que eu leve a FÉ. Onde houver desespero, que eu leve a ESPERANÇA. Onde houver ódio que eu leve o AMOR" (Oração pela Paz)

SJ.Jafari

unread,
Aug 16, 2010, 9:51:22 AM8/16/10
to struts2-jquery
hi
I added the key column too and it didn't work.
anyway I kinda changed my programs flow but the exclude properties of
json seems pretty plausible.
thanks.
Reply all
Reply to author
Forward
0 new messages