Constructing resultMap to create different instances for the same collection

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Constructing resultMap to create different instances for the same collection

Mark
(Using MyBatis v3.0.4.)
I have a problem that I do not know how to solve. My object model is:

//Location.java
public class Location {
    // ... other content
    private List<Address> addresses;
    // ... other content
}

//Address.java
public class Address {
    public enum Type { POSTAL, POBOX, INPUT, CLEANSED }
    private Type type;
    private String line1;
    // ... other content
}

My SQL is:
SELECT
    // ... other content
    postal_address_line_1,
    postal_address_line_2,
    postal_address_city,
    cleansed_address_line_1,
    cleansed_address_line_2,
    cleansed_address_city,
    // ... other content

How would I construct a resultMap that would plug the appropriate
columns into an address instance of the correct type and added to the
same list in Location.java? I would like to avoid having to add
another instance variable to Location.java just to hold a different
type of address.

Thanks very much in advance,
Mark.
Reply | Threaded
Open this post in threaded view
|

Re: Constructing resultMap to create different instances for the same collection

Nicolai Willems
On Thu, May 19, 2011 at 9:42 AM, Mark <[hidden email]> wrote:

> (Using MyBatis v3.0.4.)
> I have a problem that I do not know how to solve. My object model is:
>
> //Location.java
> public class Location {
>    // ... other content
>    private List<Address> addresses;
>    // ... other content
> }
>
> //Address.java
> public class Address {
>    public enum Type { POSTAL, POBOX, INPUT, CLEANSED }
>    private Type type;
>    private String line1;
>    // ... other content
> }
>
> My SQL is:
> SELECT
>    // ... other content
>    postal_address_line_1,
>    postal_address_line_2,
>    postal_address_city,
>    cleansed_address_line_1,
>    cleansed_address_line_2,
>    cleansed_address_city,
>    // ... other content
>
> How would I construct a resultMap that would plug the appropriate
> columns into an address instance of the correct type and added to the
> same list in Location.java? I would like to avoid having to add
> another instance variable to Location.java just to hold a different
> type of address.
>
> Thanks very much in advance,
> Mark.

Hi Mark

I don't know if you found a solution, but as far as I understand
MyBatis, this is a case for discrimnators,
have you had a look at them?

/Willems