(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 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.
Along the lines of Andy Pryor’s suggestion, I was able to solve the problem by updating my SQL statement to something like the following:
Then update my
resultMapto the following:Finally, within my
Addressclass I am able to havesetType(Type)and the inbuilt enumerated type handler does the magic. Within theLocationclass I can just have one list of instances ofAddressand the various setXXXAddress() methods can add to this list appropriately.It is unfortunate that I cannot plug the columns into some sort of factory class but putting hard-coded types into the SQL statement isn’t too dirty, in my opinion. The disadvantage is that I have introduced coupling between the domain model’s
Address.Typevalues and the SQL statement but this is kind of already there given that theresultMapSQL XML needs to hold the names of instance variables in theAddressclass anyway.