views:

197

answers:

1

Is even possible to bind an root object with super fields?

I have quite complicated entity (entities) to bind. I am already lost with it :)

This is my code situation:

public class CoreRestWrapper {
    private Integer page;
    private Integer perPage;
}

public class UserWrapper extends CoreRestWrapper {
    private Collection<UserCVO> users;
}

public class UserCVO  {
    private UserVO userVO;
    private Map<String, UserMeta> meta;
}

public class UserVO extends BaseVO {
    //nothing here
}

public class BaseVO {
    private Integer id;
    private String mainName;
}

public class UserMeta extends Meta {
    //nothing here
}

public class Meta {
    private String key;
    private Object value;
    private String type; //String, Integer, Boolean, Date
}

UserWrapper is my root object. I would like to have my response xml look like this:

<users>
    <page>1</page>
    <perPage>2</perPage>
    <user>
     <id>1</id>
     <main>test</main>
     <meta type="String" key="wtf">just for fun</meta>
     <meta type="Integer" key="age">99</meta>
    </user>
    <user>
     <id>2</id>
     <main>test</main>
     <meta type="String" key="wtf">fooooo</meta>
     <meta type="Integer" key="age">101</meta>
    </user>
</users>

Or maybe even <users page="1" perPage="2"> and then just user tags...

Is this even possible (for a beginner in JiBX to handle)?

+1  A: 

You can, but you have to define mappings for everything in the hierarchy. I ran into the same thing. It ended up complex enough that I found it simpler to just create a JIBX wrapper object to map to (e.g. UserJibxWrapper), then write a little code to map that into the internal objects.

In your case though, the object themselves are fairly simple, so you might be able to generate all the mappings needed. I'd work top down. Take the base class and map it, get that mapping work, then add in a subclass and alter your mappings as needed, and keep working down that way. Since JIBX needs to know everything in the hierarchy to map, it's easier to map top-down than bottom-up.

Chris Kessel
I did exactly like you wrote. I just need two days to figure JiBX mapping bindings and costume marshalling. And yes - my problem was that I started bottom-up, that's why I got lost :)
Trick
Congratulations :). It's certainly not easy, but once you learn some of the oddities in the mappings it's a pretty nice way of handling XML to object conversion. In some ways, it reminded me of the learning curve in creating Hibernate mappings.
Chris Kessel