Stripes

JavaScriptBuilder should allow exclusion of properties by name

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: Release 1.3.2, Release 1.4
  • Fix Version/s: Release 1.5
  • Component/s: None
  • Labels:
    None
  • Environment:
    Win32/JDK1.5

Description

Re-titling issue. The original problem (converting hibernate proxied objects) has been fixed with an arguably simpler solution for 1.4.1. See STS-264.

However, the possibility to exclude properties by name is also something that would be useful independent of hibernate, so I'm leaving this targetted for 1.5 with that purpose in mind.

--------------------------------------------------------------------------------------------------

If an object is loaded via Hibernate and then passed to the JavaScriptBuilder, it throws various exceptions.

See http://article.gmane.org/gmane.comp.java.stripes.user/769/ for a report of the error - to summarize:

Activity

Hide
Simon added a comment - 21/Aug/06 4:50 AM

(continued from above)

example stack trace:

> The stack trace:
>
> net.sourceforge.stripes.exception.StripesRuntimeException: Could not
> build JavaScript for object. An exception was thrown while trying to
> convert a property from Java to JavaScript. The object being converted
> is: com.kembuco.autotracker.model.Automobile <at> b36ee3db
> at net.sourceforge.stripes.ajax.JavaScriptBuilder.build(JavaScriptBuilder.java:154)
> at net.sourceforge.stripes.ajax.JavaScriptResolution.execute(JavaScriptResolution.java:50)
> at net.sourceforge.stripes.controller.DispatcherServlet.doPost(DispatcherServlet.java:168)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)

The problem here is that rather than exporting the actual object the loaded object is enhanced by Hibernate with a variety of internal attributes that don't export properly for various reasons.

Rather than suggesting that we specifically make Stripes work with Hibernate, I'd suggest that a simple generic interface be supported to allow the caller to suppy an arbitrary algorithm for excluding properties.

I have attached a patch which illustrates an implementation of this which allows Hibernate objects to export correctly simply by ignoring the internal hibernate properties by excluding them by name.

Using this patch, Hibernate objects can be passed to JavaScriptBuilder successfully with a construct like:

return new JavaScriptResolution(hibnerateObject, new ExcludeByName(new String[] { "callback", "callbacks", "hibernateLazyInitializer" }));

Hope this is helpful. I think it's important for Stripes to be able to send back Hibernate objects as Javascript.

(see separately attached patch file)

Show
Simon added a comment - 21/Aug/06 4:50 AM (continued from above) example stack trace: > The stack trace: > > net.sourceforge.stripes.exception.StripesRuntimeException: Could not > build JavaScript for object. An exception was thrown while trying to > convert a property from Java to JavaScript. The object being converted > is: com.kembuco.autotracker.model.Automobile <at> b36ee3db > at net.sourceforge.stripes.ajax.JavaScriptBuilder.build(JavaScriptBuilder.java:154) > at net.sourceforge.stripes.ajax.JavaScriptResolution.execute(JavaScriptResolution.java:50) > at net.sourceforge.stripes.controller.DispatcherServlet.doPost(DispatcherServlet.java:168) > at javax.servlet.http.HttpServlet.service(HttpServlet.java:709) The problem here is that rather than exporting the actual object the loaded object is enhanced by Hibernate with a variety of internal attributes that don't export properly for various reasons. Rather than suggesting that we specifically make Stripes work with Hibernate, I'd suggest that a simple generic interface be supported to allow the caller to suppy an arbitrary algorithm for excluding properties. I have attached a patch which illustrates an implementation of this which allows Hibernate objects to export correctly simply by ignoring the internal hibernate properties by excluding them by name. Using this patch, Hibernate objects can be passed to JavaScriptBuilder successfully with a construct like: return new JavaScriptResolution(hibnerateObject, new ExcludeByName(new String[] { "callback", "callbacks", "hibernateLazyInitializer" })); Hope this is helpful. I think it's important for Stripes to be able to send back Hibernate objects as Javascript. (see separately attached patch file)
Hide
Simon added a comment - 21/Aug/06 4:52 AM

Apply patch in stripes/src/net/sourceforge/stripes/ajax directory

eg. patch < propertyexcluder.patch

Show
Simon added a comment - 21/Aug/06 4:52 AM Apply patch in stripes/src/net/sourceforge/stripes/ajax directory eg. patch < propertyexcluder.patch
Hide
Tim Fennell added a comment - 02/Sep/06 9:17 AM

I'm assigning this to 1.5 since I'd like to take some time to investigate alternative options too. For example, if the hibernate classes report themselves as synthetic, it might be easy to avoid their property's by default.

On the other hand, if there is an small-scale fix that doesn't change the interface, I wouldn't mind sliding it back into 1.4.x.

Show
Tim Fennell added a comment - 02/Sep/06 9:17 AM I'm assigning this to 1.5 since I'd like to take some time to investigate alternative options too. For example, if the hibernate classes report themselves as synthetic, it might be easy to avoid their property's by default. On the other hand, if there is an small-scale fix that doesn't change the interface, I wouldn't mind sliding it back into 1.4.x.
Hide
Charlie Hubbard added a comment - 29/Sep/07 7:11 AM

Actually you can use a libary called flexjson (http://fllexjson.sourceforge.net) right now with 1.4.x and get this behavior in one line. Creating JSON from your objects you've read with Hibernate is really simple. Here is an example from my stripes app:

<pre>
public Resolution search() throws ParseException { Map messages = doSearch(); String json = new JSONSerializer() .transform( new HTMLEncoder(), "to", "from", "cc", "bcc" ) .include("attachments") .exclude("searchText", "contentType", "mailbox", "*.class") .serialize( messages ); return toJson( json ); }
</pre>

Show
Charlie Hubbard added a comment - 29/Sep/07 7:11 AM Actually you can use a libary called flexjson (http://fllexjson.sourceforge.net) right now with 1.4.x and get this behavior in one line. Creating JSON from your objects you've read with Hibernate is really simple. Here is an example from my stripes app: <pre> public Resolution search() throws ParseException { Map messages = doSearch(); String json = new JSONSerializer() .transform( new HTMLEncoder(), "to", "from", "cc", "bcc" ) .include("attachments") .exclude("searchText", "contentType", "mailbox", "*.class") .serialize( messages ); return toJson( json ); } </pre>
Hide
Aaron Porter added a comment - 07/Jan/08 3:37 PM

you may now pass in class types or property names to exclude the type or property

Show
Aaron Porter added a comment - 07/Jan/08 3:37 PM you may now pass in class types or property names to exclude the type or property
Hide
Paulo Silva added a comment - 22/Jan/10 11:59 AM

"you may now pass in class types or property names to exclude the type or property"

How can you exclude correctly the hibernate stuff using this? The original patch provided an ExcludeByName that would work, the exclude per property doesn't...

Show
Paulo Silva added a comment - 22/Jan/10 11:59 AM "you may now pass in class types or property names to exclude the type or property" How can you exclude correctly the hibernate stuff using this? The original patch provided an ExcludeByName that would work, the exclude per property doesn't...

People

Vote (0)
Watch (0)

Dates

  • Created:
    21/Aug/06 4:34 AM
    Updated:
    04/Jan/11 3:13 PM
    Resolved:
    07/Jan/08 3:37 PM