<p>This is not specific to any language, it´s just about best practices. I am using JPA/Hibernate (but it could be any other ORM solution) and I would like to know how do you guys deal with this situation:
Let´s suppose that you have a query returning something that is not represented by any of your domain classes.
Do you create a specific class to represent that specific query?
Do you return the query in some other kind of object (array, map...)
Some other solutions?
I would like to know about your experiences and best practices.</p>

<p>P.S.
Actually I am creating specific objetcs for specific queries.</p>

[{'answer_id': 51726, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 0, 'selected': False, 'text': "<p>I typically write a function that performs a query using SQL and then puts the results into either a list or dictionary (in Java, I'd use either an ArrayList or a HashMap).</p>\n\n<p>If I found myself doing this a lot, I'd probably create a new file to hold all of these queries.  Otherwise I'd just make them functions in whatever file they were needed/used.</p>\n\n<p>Since we're talking Java specifically, I would certainly not create a new class in a separate file.  However, for queries needed in only one class, you could create a private static inner class with only the function(s) needed to generate the query(s) needed by that class.</p>\n"}, {'answer_id': 51757, 'author': 'nso1', 'author_id': 3027, 'author_profile': 'https://Stackoverflow.com/users/3027', 'pm_score': 0, 'selected': False, 'text': '<p>The idea of wrapping that up the functionality in some sort of manager is always nice.  It allows for better testing, and management therefore of schema changes.</p>\n\n<p>Also allows for easier reuse in the application.  NEVER just put the sql in directly!!!.  For Hibernate I have found HQL great for just this.  In particular , if you can use Named queries.  Also be careful of adding an filter values etc use "string append", use parameters (can we say SQL injection ?). Even if the SQL is dynamic in terms of the join or where criteria, have a function in some sort of manager is always best.</p>\n'}, {'answer_id': 51758, 'author': 'Dave Richardson', 'author_id': 3392, 'author_profile': 'https://Stackoverflow.com/users/3392', 'pm_score': 1, 'selected': False, 'text': '<p>We have a situation that sounds similar to yours.</p>\n\n<p>We use separate objects for reporting data that spans several domain objects. Our convention is that these will be backed by a view in the database, so we have come to call them view objects. We generally use them for summarising complex data into a flat format.</p>\n'}, {'answer_id': 51764, 'author': 'Ivan Bosnic', 'author_id': 3221, 'author_profile': 'https://Stackoverflow.com/users/3221', 'pm_score': 0, 'selected': False, 'text': '<p>@DrPizza</p>\n\n<p>I will be more specific. We have three tables in a database</p>\n\n<pre><code>USER\nPROJECT\nTASK\nUSER to TASK 1:n\nPROJECT to TASK 1:n\n</code></pre>\n\n<p>I have a query that returns a list of all projects but showing also some grouped information (all tasks, open tasks, closed tasks). When returned, the query looks like this</p>\n\n<pre><code>PROJECTID: 1\nNAME: New Web Site\nALLTASK: 10\nOPENTASK: 7\nCLOSEDTASK: 3\n</code></pre>\n\n<p>I don´t have any domain class that could represent this information and I don´t want to create specific methods in Project class (like getAllTasks, getOpenTasks) because each of these methods would trigger a new query.\nSo the question is:\nI create a new class (somenthing like ProjectTasksQuery) just to hold that information?\nI return information within array or map?\nSomething else?</p>\n'}, {'answer_id': 52044, 'author': 'Joe Liversedge', 'author_id': 4552, 'author_profile': 'https://Stackoverflow.com/users/4552', 'pm_score': 0, 'selected': False, 'text': '<p>You might feel better after reading about <a href="http://en.wikipedia.org/wiki/Data_Transfer_Object" rel="nofollow noreferrer">Data Transfer Objects</a>. Some people <a href="http://anirudhvyas.com/root/2008/04/19/abuses-of-dto-pattern-in-java-world/" rel="nofollow noreferrer">plain don\'t like them</a>, but if it feels like a good fit to you, it probably is.</p>\n'}]
['https://Stackoverflow.com/questions/51653', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3221/']