Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have studied some persistence frameworks mainly Hibernate, DORM, TMS Aurelius and I'm wondering. In a model of type:

TOrderFiscal < > ----- > Items < > -------- > TOrderItem ------- > TProduct

TOrderFiscal --------- > TSuplier --------- > TCidade -------- > State

Each class of this with countless attributes, all mapped correctly to their corresponding tables and fields. When carrying a note without using "Lazy Load" would always be done with several joins select all fields to come from each of these classes.

But it would be a waste since the note need only the supplier code and name attributes, the attributes OrderItem need only code, qty name. stock.

What solution should be taken to create a specific class supplier of note just with these attributes ? eg

TOrder <> ----- > Items <> -------- > TOrderItem ------- > TNotaItemProduct

TOrder -------- > TOrderSupplier

If this is the solution , what happens when a business rule in TProduct should also be TOrderItemProduct? I duplicate code in both classes? Do heritage, eg TORderItemProduct and TProduct both inheriting from TBaseProduct ?

Or is this a concern that we programmers we should not have ?

share|improve this question
This is one of the reasons I do not like ORM frameworks. –  Brandon Apr 12 at 17:29
@Brandon The problem is not the ORM the problem is how its being used. Domain objects should not be mapped to a db using an ORM. –  Andy Apr 12 at 19:58
@Andy: I thought that was kinda the whole point of an ORM. –  Robert Harvey Apr 13 at 13:55
@RobertHarvey It depends on who you ask. For trivial apps sure, but for more complex apps mapping your business / domain objects directly forces you to design your business / domain objects around the data, and reuse becomes about data, not behavior. I like ORMs, if used correctly, because you have a strongly typed layer to access your data which can easily be code genned. You can then really quickly find out that some column has disappeared by compiling. –  Andy Apr 13 at 21:57
@Andy If you do it like that, then you are using ORM wrong. You should design your mappings based on the domain, not other way around. –  Euphoric Jul 14 at 5:37

1 Answer 1

This is what we use the Hibernate Query Language (hql) for - to select only the desired data when the entire object web is not needed. For example:

select new map (s.code as sCode, s.name as sName, i.code as iCode, i.qty as qty, i.name as iName, i.stock as stock)
from TOrderFiscal tof 
join tof.suplier s
join tof.items i
join i.product p
where p.id = 12345;

You will get back a list of map of string to object, where each item in the list is a row in the result set represented as a map of the selected field aliases to the field values.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.