• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

toString() and Comparable<T> on a JAXB generated entity class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm trying to create a distributed system and therefore I have all my entity classes present in my Java EE app and hosted as a web service. The Swing frontend app autogenerate the entity classes for use in the presentation.
I'm having some trouble using the Collections.sort() since a generated entity cannot implement an interface, does anyone have a hint to how i can solve this issue?
Also, I need to override the toString() so that I can put an entity directly in a eg. JCombobox and it is presented with the return of my custom toString() but if I override the toString() in my original entity in my backend app, then this method is not generated in my frontend app.

Thanks in advance!

Nicklas
 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Do you mean that the Swing frontend generates the entity classes on the fly - that is when the program is running?

Both of you problems can be solved by either:
- Repacking the entities received by the client in classes that implement Comparable and that have toString methods.
- Creating wrapper classes for each entity class that implement Comparable, implements a toString method and delegates data retrieval etc to the wrapped entity class.

Regarding classes generated by JAXB: My advice is to see them as helpers in conveying the state of, in your case, entities going over the wire from a server to a client. Such classes will help you marshal and unmarshal data, but not much more. This is not serialization and the data passed from client to server only describes the state of objects, the object itself is not transferred.
Best wishes!
 
Nicklas Jepsen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply!

However, I'm not sure I perfectly understand your solution.

Do you mean that the Swing frontend generates the entity classes on the fly - that is when the program is running?


No, that is not what happens.
The classes are generated when i build the app. The WSDL is downloaded and the classes are generated.

Repacking the entities received by the client in classes that implement Comparable and that have toString methods.


Could you please explain what you mean by repacking the entities?
Do you mean that I should create classes in the frontend app identical to the ones in my backend/web service?

Again, thank you for your help on this topic which I really has been stucked on for a while now
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicklas Jepsen wrote:

Repacking the entities received by the client in classes that implement Comparable and that have toString methods.


Could you please explain what you mean by repacking the entities?
Do you mean that I should create classes in the frontend app identical to the ones in my backend/web service?


Creating identical classes is of little use, the point is to create classes that have the additional functionality etc that you want to use in the client.

Lets say you have a JAXB-generated class named EntytyA, which contains the properties propertyOne and propertyTwo.
- If you want to repack, you create another class, lets call it RepackedEntityA.
This class will have the same properties as EntityA and implement any interface(s) you want it to implement, as well as have any extra methods you want it to have.
When receiving an EntityA instance as a result of a request to the service, you use getter and setter methods to transfer the data in the EntityA instance to an instance of RepackedEntityA.
The instance of RepackedEntityA is the used throughout the client.
- If you want to wrap, you create another class, lets call it EntityAWrapper.
This class will not contain propertyOne or propertyTwo, but instead an instance of EntityA. It can implement any additional interface(s) etc etc.
When calling the getter method to retrieve, for instance, propertyOne, from an instance of EntityAWrapper, the instance delegates the retrieval of the property to the contained instance of EntityA.
Hope things are more clear!
 
Nicklas Jepsen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your quick reply!

That did clear things up for me, but just to be sure that I really understand what you are saying lets say I have the following:
class EntityA with a list og EntityB
When i call getAllA() on the webservice, then I would need to iterate throu all EntityA and EntityB an repack them to my RepackedEntityA and RepackedEntityB classes and then have my method in my frontend app to, say, return List<RepackedEntityA> and not just return List<EntityA>?

Hope it makes sence, otherwise let me know, and I will try and post some source.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Nicklas Jepsen wrote:
When i call getAllA() on the webservice, then I would need to iterate throu all EntityA and EntityB an repack them to my RepackedEntityA and RepackedEntityB classes and then have my method in my frontend app to, say, return List<RepackedEntityA> and not just return List<EntityA>?


Right!
Do also take a look at the JAXB guide, especially chapter 3: https://jaxb.dev.java.net/guide/
I am thinking that if you create a subclass of a generated class, there may be a way to make JAXB create instances of that class instead of the originally generated class.
https://jaxb.dev.java.net/guide/
I was first under the impression that you had a more dynamic scenario, but it seems it is not as dynamic as I first thought, so perhaps this is a viable approach.
Best wishes!
 
Nicklas Jepsen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your help. I think I will go on with the "repack solution" since I already have been reading alot on the JAXB guide you linked to.

If anyone knows a quick way on having JAXB to map the generated classes to my own implementations, then I would like to know.

But thanks Ivan, your help is much appreciated
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
On my way to work, I came to think of another solution:
You can use AOP, specifically AspectJ, to make your existing classes implement the Comparable interface and also introduce any additional methods, such as toString. This way you do not have to write any additional classes and can use the generated classes.
If you want to know more about this solution, let me know.
Best wishes!
 
Nicklas Jepsen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you want to know more about this solution, let me know.



If it is a solution not to hard to implement, then I would love to hear more about AspejtJ. But if it takes a lot of time to set up, then I think I'll stick to the "wrapper solution".
But AOP definetely sounds interesting, my only concern is that I'm on a tight schedule and therefor I do not have the time to do a heavy refactoring at the moment.

Anyway, thank you for letting me know about AOP and AspectJ.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again!
I have created a small sample program that mimics your conditions. In it there is one single entity class that, originally, does not implement the Comparable interface and does not have a toString method. Using AspectJ, I add the Comparable interface to the parents of the class and add the toString and compareTo methods to the class.
Development was done in Eclipse with the latest version of the ADT plugin installed.
First, the entity class:


Second, the main program:


Finally, the aspect:

Best wishes!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic