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

  • Home
  • SEARCH
  • 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 6871363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:47:48+00:00 2026-05-27T03:47:48+00:00

How is it possible to keep clean layers with Hibernate/ORM (or other ORMs…)? What

  • 0

How is it possible to keep clean layers with Hibernate/ORM (or other ORMs…)?

What I mean by clean layer separation is for exemple to keep all of the Hibernate stuff in the DAO layer.

For example, when creating a big CSV export stream, we should often do some Hibernate operations like evict to avoid OutOfMemory… The filling of the outputstream belong to the view, but the evict belongs to the DAO.

What I mean is that we are not supposed to put evict operations in the frontend / service, and neither we are supposed to put business logic in the DAO… Thus what can we do in such situations?

There are many cases where you have to do some stuff like evict, flush, clear, refresh, particularly when you play a bit with transactions, large data or things like that…

So how do you do to keep clear layers separation with an ORM tool like Hibernate?


Edit: something I don’t like either at work is that we have a custom abstract DAO that permits a service to give an Hibernate criterion as an argument. This is practical, but for me in theory a service that calls this DAO shouldn’t be aware of a criterion. I mean, we shouldn’t have in any way to import Hibernate stuff into the business / view logic.


Is there an answer, simple or otherwise?

  • 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-27T03:47:49+00:00Added an answer on May 27, 2026 at 3:47 am

    If by “clean” you mean that upper layers don’t know about implementations of the lower layers, you can usually apply the
    Tell, don’t ask principle. For your CSV streaming example, it would be something like, say:

    // This is a "global" API (meaning it is visible to all layers). This is ok as
    // it is a specification and not an implementation.
    public interface FooWriter {
        void write(Foo foo);
    }
    
    // DAO layer
    public class FooDaoImpl {
        ...
        public void streamBigQueryTo(FooWriter fooWriter, ...) {
            ...
            for (Foo foo: executeQueryThatReturnsLotsOfFoos(...)) {
                fooWriter.write(foo);
                evict(foo);
            }
        } 
        ...
    }
    
    // UI layer
    public class FooUI {
        ...
        public void dumpCsv(...) {
            ...
            fooBusiness.streamBigQueryTo(new CsvFooWriter(request.getOutputStream()), ...);
            ...
        }
    }
    
    // Business layer
    public class FooBusinessImpl {
        ...
        public void streamBigQueryTo(FooWriter fooWriter, ...) {
            ...
            if (user.canQueryFoos()) {
                beginTransaction();
                fooDao.streamBigQueryTo(fooWriter, ...);
                auditAccess(...);
                endTransaction();
            }
            ...
        }
    }
    

    In this way you can deal with your specific ORM with freedom. The downside of this “callback” approach: if your layers are on different JVMs then it might not be very workable (in the example you would need to be able to serialize CsvFooWriter).

    About generic DAOs: I have never felt the need, most object access patterns I have found are different enough to make an specific implementation desirable. But certainly doing layer separation and forcing the business layer to create Hibernate criteria are contradictory paths. I would specify a different query method in the DAO layer for each different query, and then I would let the DAO implementation get the results in whatever way it might choose (criteria, query language, raw SQL, …). So instead of:

    public class FooDaoImpl extends AbstractDao<Foo> {
        ...
        public Collection<Foo> getByCriteria(Criteria criteria) {
            ...
        }
    }
    
    public class FooBusinessImpl {
        ...
        public void doSomethingWithFoosBetween(Date from, Date to) {
            ...
            Criteria criteria = ...;
    
            // Build your criteria to get only foos between from and to
    
            Collection<Foo> foos = fooDaoImpl.getByCriteria(criteria);
            ...
        }
    
        public void doSomethingWithActiveFoos() {
            ...
            Criteria criteria = ...;
    
            // Build your criteria to filter out passive foos
    
            Collection<Foo> foos = fooDaoImpl.getByCriteria(criteria);
            ...
        }
        ...
    }
    

    I would do:

    public class FooDaoImpl {
        ...
        public Collection<Foo> getFoosBetween(Date from ,Date to) {
            // build and execute query according to from and to
        }
    
        public Collection<Foo> getActiveFoos() {
            // build and execute query to get active foos
        }
    }
    
    public class FooBusinessImpl {
        ...
        public void doSomethingWithFoosBetween(Date from, Date to) {
            ...      
            Collection<Foo> foos = fooDaoImpl.getFoosBetween(from, to);
            ...
        }
    
        public void doSomethingWithActiveFoos() {
            ...
            Collection<Foo> foos = fooDaoImpl.getActiveFoos();
            ...
        }
        ...
    }
    

    Though someone could think that I’m pushing some business logic down to the DAO layer, it seems a better approach to me: changing the ORM implementation to an alternative one would be easier this way. Imagine, for example that for performance reasons you need to read Foos using raw JDBC to access some vendor-specific extension: with the generic DAO approach you would need to change both the business and DAO layers. With this approach you would just reimplement the DAO layer.

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

Sidebar

Related Questions

Is possible to delete bundles not needed in order to keep the project clean?
Using MongoDB I want to keep my model as clean as possible so I
Generally i like to keep my database as clean and expandable as possible. However
I am attempting to keep user inputs into our application as clean as possible.
I am trying to keep my code as secure/clean as possible. If I have
In order to keep the main() (in c++) as clean/small as possible, there a
i'm trying to understand how and if its possible to keep a session open
I want to change color of the UINavigationBar? Is it possible to keep different
Is it possible to always keep selected item in the middle of a listbox?
I keep seeing documentation saying that its not possible to send to a remote

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.