iBatis circular references question
I have an Office object and a Device Object. An Office has a device and a device belongs to an office. So, obtaining an Office object should populate Office.Device reference and a Device object should have an Office object where it belongs. It's a circular refer开发者_如何学JAVAence I suppose.
I'd like to do this in iBATIS, but I can't figure out how to do it properly.
It is almost like I need a resultMap where I can "set this property to the object created by the resultMap which just called me". Rather then populate this property (object) with another query (N+1, I know) or populate this property using the resultset and this other resultMap.
I don't mind creating another resultMap for this scenario.
Another perfectly acceptable answer is that iBATIS doesn't do this because your schema is flawed. If that is the case, give me some other options.
If the Device is only required to have a circular reference to the Office that it exists within and there is no scenario where an office can contain a device and that device can contain a list of further offices.
Then a RowHandler would probably do the job. Query the office and device via a the SQL map in the normal manner and satisfy the office to the device relationship. Then within the the RowhHandler associated with the query call populate a reference the containing office object within a member of the device, thereby making the circular reference and requiring no extra queries.
http://www.javalobby.org/java/forums/t75161.html has an example near the bottom of including the result of another resultMap as a property. I've never done this but from his example it seems like it would work. You would probably have 4 sql queries: 1 for the office and a related one that pulls the associated device, then the corresponding device query with one that pulls the associated office. Hope this helps.
精彩评论