Is it possible in myBatis 3 to map a single result to multiple objects, ensuring that the objects all reference the same instance? Is there an example of this I could reference?
Updated to add more detail:
For instance, let’s say I store information regarding Contacts for my application in my DB. I want to know if it’s possible to use myBatis to map the same instance of a contact to, say, a Listing class, which holds a Contact:
public class Listing {
private Contact myContact;
//getters & setters...
}
as well as to a ContactsHolder class, which also holds a Contact:
public class ContactsHolder {
private Contact aContact
//getters & setters...
}
I need the object that is mapped by myBatis to both the Listing and ContactsHolder classes to be the same instance. Is this possible?
No, MyBatis isn’t able to do that with standard result mapping. (at least to my knowledge). You could select the “Contact” object, then build a Listing and ContactsHolder manually with both of them referencing the Contact.
Or implement a custom ResultSetHandler.
It’s kind of a peculiar request, I’m not sure why you want the same instances shared across two objects like that. That’s probably why no feature like this exists in MyBatis 3.