Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6114957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:02:09+00:00 2026-05-23T15:02:09+00:00

I have a domain object which represents a 1:n relationship between database tables. public

  • 0

I have a domain object which represents a 1:n relationship between database tables.

public class ObservationWithData {
   private Observation observation;
   private Map<Integer,ElementValue> elementValues;
   // accessor methods
... }

Both Observation and ElementValue contain

   private int datetimeId;
   private int stationId;
   // accessor methods
   ...

I have a selection query which joins the two tables. I’d like to use a resultMap to group the ElementValues with the Observations based on the datetimeId and stationId, as in this (nonworking) example.

<resultMap id="observationWithDataMap" class="ObservationWithData" 
           groupBy="observation.datetimeId,observation.stationId">
  <result property="observation" resultMap="ObservationSql.observationMap" />
  <result property="elementValues" resultMap="ElementSql.elementValueMap"/>
</resultMap>

There are two problems with this. First, the groupBy tag does not allow the nested syntax, and second iBATIS requires the grouped property to be part of the Java Collections API, and Map does not meet that criterion.

I can work around the first problem by adding datetimeId and stationId accessors, and the second item can be solved by creating a collection to write to and then add all the items to the map after the data pull.

<resultMap id="observationWithDataMap" class="ObservationWithData" 
           groupBy="datetimeId,stationId">
  <result property="stationId" column="station_id" />
  <result property="datetimeId" column="datetime_id" />
  <result property="observation" resultMap="ObservationSql.observationMap" />
  <result property="elementValueCollection" resultMap="ElementSql.elementValueMap"/>
</resultMap>

public class ObservationWithData {
   private Observation observation;
   private Map<Integer,ElementValue> elementValues;
   // this collection is used for database retrieval only; do not add to it
   private Collection<ElementValue> elementValueCollection;

   public void setStationId(int id) { }
   public int getStationId() {
   return observation==null?0:observation.getStationId();
   }
   public void setDatetimeId(int id) { }
   public int getDatetimeId() {
   return observation==null?0:observation.getDatetimeId();
   }

   public Map<Integer,ElementValue> getElementValues() {
   if (elementValues.size()==0 && elementValueCollection.size()>0) {
       for (ElementValue val : elementValueCollection) {
           elementValues.put(val.getElementId(), val);
       }
       elementValueCollection.clear();
   }
   return elementValues;
   }

   public Collection<ElementValue> getElementValueCollection() {
   if (elementValueCollection.size()==0 && elementValues.size()>0) {
       elementValueCollection.addAll(elementValues.values());
       elementValues.clear();
   }
   return elementValueCollection;
   }
   ... }

I’m not very happy with this solution, though, because now there are a lot of public methods that I don’t want people to use in their code. The id setters are noops because these ids get set in the observation. I could have ObservationWithData extend Observation, but I designed this class to favor composition over inheritance per Effective Java. I could also synchronize the map and collection differently, but the idea is that I’ll let iBATIS populate the collection, and then when the map is accessed, I’ll move all the values into it, keeping just one reference.

Can someone recommend a more elegant solution?

Edit:

I ended up extracting a service layer to handle this. The service layer makes two database calls through the DAOs instead of one; the first retrieves all the Observations and the second retrieves all the ElementValues. It constructs the ObservationWithData objects from these collections. It also throttles the request since this is a large dataset.

This is a little clunky and inefficient, because I’m “manually” constructing the objects. However, since this is “hidden” in the service layer, I feel that it’s less intrusive to the API users, who get an uncluttered domain object to work with.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:02:10+00:00Added an answer on May 23, 2026 at 3:02 pm

    Did you try writing your DAO so that this is a 2-stage proces, where you first fetch the ObservationWithData objects and then fill them with elementValues? It will not scale for huge numbers of rows, but depending on what you need to fetch it may make it a little cleaner on the outside for now.

    Another option would be to fetch the data from the database by mapping results to package private objects with getters and setters, and then returning them to the caller as different class without the setters. Not sure if that will help though, I think it maybe even add more mess to the solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So let's say we have a domain object such as the following public class
Let's say I have an interface representing a domain object: public interface IFoo {
My use case is simple. I have a root domain object which references a
I have a XPO domain object Order which has a association with OrderLine. In
I have the following domain object : class DbDeployment { static constraints = {
I have a basic domain object, say like Person or Campaign or Event that
I have a property on a domain object that is declared in a many-to-one
I have the current basic structure for each domain object that I need to
I have the following models. # app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name
I have a domain class containing a couple of fields. I can access them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.