16

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419

11 Answers11

6

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.

gpeche
  • 21,974
  • 5
  • 38
  • 51
  • Nice answer. Like the "tell don't ask", this seems related to what Guillaume said. About criterions i totally agree, i don't like that generic dao to expose criterions in the interface. But it's not a generic dao problem because without exposing criterions, the generic dao crud operations using hibernate could easily be replaced by plain old sql – Sebastien Lorber Nov 23 '11 at 11:13
  • What i mean is that people tend to include a `queryByCriteria(Criteria c)` in generic DAOS as a unified entry point for all queries. I prefer to make specific entry points for specific queries. It can be more work but is usually easier to maintain. – gpeche Nov 23 '11 at 22:44
  • Guillaume's answer is similiar, but he seems to prefer more fine-grained interfaces. My point of view is usually "the coarser the better". – gpeche Nov 23 '11 at 22:46
5

Well, you can always tell your DAO layer to do what it needs to do when you want to. Having a method like cleanUpDatasourceCache in your DAO layer, or something similar (or even a set of these methods for different objects), is not bad practice to me.

And your service layer is then able to call that method without any assumption on what is done by the DAO under the hood. A specific implementation which uses direct JDBC calls would do nothing in that method.

Guillaume
  • 22,694
  • 14
  • 56
  • 70
4

Usually a DAO layer to wrap the data access logic is necessary. Other times is just the EntityManager what you want to use for CRUD operations, for those cases, I wouldn't use a DAO as it would add unnecessary complexity to the code.

How should EntityManager be used in a nicely decoupled service layer and data access layer?

Community
  • 1
  • 1
2

If you don't want to tie your code to Hibernate you can use Hibernate through JPA instead and not bother too much about abstracting everything within your DAOs. You are less likely to switch from JPA to something else than replacing Hibernate.

Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76
  • that's true but it's not what i'd like to know... if you prefer my question is: "how do you do to keep a clean dao layer where you can switch easily from jpa to nosql for exemple? without having to modify anything else than the dao – Sebastien Lorber Nov 04 '11 at 18:56
2

my 2 cents: i think the layer separation pattern is great as a starting point for most cases, but there is a point where we have to analyze each specific application case by case and design a more flexible solution. what i mean is, ask yourself for example:

  • is your DAO expected to be reused in another context other than exporting csv data?

  • does it make sense to have another implementation of the same DAO
    interface without hibernate ?

if both answers were no, maybe a little bit of coupling between persistence and data presentation is ok. i like the callback solution proposed above.

IMHO sometimes strict implementation of a pattern has a higher cost in readability, mantainability, etc. which are the very issues we were trying to fix by adopting a pattern in the first place

jambriz
  • 1,273
  • 1
  • 10
  • 25
  • That's true. Anyway i don't think we will ever switch from hibernate to something else since all the app is kinda already coupled to hibernate advanced features – Sebastien Lorber Nov 24 '11 at 10:48
1

You got some good answers here, I would like to add my thoughts on this (by the way, this is something to take care of in our code as well) I would also like to focus on the issue of having Hibernate annotations/JPA annotations on entities that you might need to use outside of your DAL (i.e - at business logic, or even send to your client side) -

A. If you use the GenericDAO pattern for a given entity, you may find your entity being annotated with Hibernate (or maybe JPA annotation) such as @Table, @ManyToOne and so on -
this means that you client code may contain Hibernate/JPA annotations and you would require an appropriate jar to get it compiled, or have some other support at your client code this is for example if you use GWT as your client (which can have support for JPA annotations in order to get entities compiled), and share the entities between the server and the client code, or if you write a Java client that performs a bean lookup using InitialContext against a Java application server (in this case you will need a JAR

B. Another approach that you can have is work with Hibernate/JPA annotated code at server side, and expose Web Services (let's say RESTFul web service or SOAP) - this way, the client works with an "interface" that does not expose knowledge on Hibernate/JPA (for example - a WSDL in case of SOAP defines the contract between the client of the service and the service itself).
By breaking the architecture to service oriented one, you get all kinds of benefits such as loose coupling, ease of replacement of pieces of code, and you can concentrate all the DAL logic in one service that serves the rest of your services, and later own replace the DAL if needed by another service.

C. You can use an "object to object" mapping framework such as dozer to map objects of classes with Hibernate/JPA annotations to what I call "true" POJOs - i.e - java beans with no annotations whatsoever on them.

D. Finally regarding annotations - why use annotations at all? Hibernate uses hbm xml files an alternative for doing the "ORM magic" - this way your classes can remain without annotations.

E. One last point - I would like to suggest you look at the stuff we did at Ovirt - you can dowload the code by git clone our repo. You will find there under engine/backend/manager/modules/bll - a maven project holding our bll logic, and under engine/backend/manager/moduled/dal - our DAL layer (although currently implemented with Spring-JDBC, and some hibernate experiments, you will get some good ideas on how to use it in your code. I would like to add that if you go for a similar solution, I suggest that you inject the DAOs in your code, and not hold them in a Singletone like we did with getXXXDao methods (this is legacy code we should strive to remove and move to injections).

Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27
  • Thanks. That's true, it's also dirty to have hibernate proxified entities in your view layer. Using annotations or hbm the result is the same for me. I prefer the option of using Dozer, it's easier and more performant than having SOAP/REST webservices to separate the layers and it permits to do what we ideally need for loose coupling: a true DTO each time your object need to be transfered from one layer to another. – Sebastien Lorber Jun 20 '12 at 13:41
1

you can achieve layer separation by implementing DAO pattern and and doing all hibernate/JDBC/JPA related stuff in Dao itself

for eg:

you can specify a Generic Dao interface as

public interface GenericDao <T, PK extends Serializable> {

/** Persist the newInstance object into database */
PK create(T newInstance);

/** Retrieve an object that was previously persisted to the database using
 *   the indicated id as primary key
 */
T read(PK id);

/** Save changes made to a persistent object.  */
void update(T transientObject);

/** Remove an object from persistent storage in the database */
void delete(T persistentObject);
}

and its implementaion as

  public class GenericDaoHibernateImpl <T, PK extends Serializable>
implements GenericDao<T, PK>, FinderExecutor {
private Class<T> type;

public GenericDaoHibernateImpl(Class<T> type) {
    this.type = type;
}

public PK create(T o) {
    return (PK) getSession().save(o);
}

public T read(PK id) {
    return (T) getSession().get(type, id);
}

public void update(T o) {
    getSession().update(o);
}

public void delete(T o) {
    getSession().delete(o);
}

}

so whenever service classes calls any method on any Dao without any assumption of the internal implementation of the method

have a look at the GenericDao link

dpsdce
  • 5,290
  • 9
  • 45
  • 58
  • Actually this is exactly what we have but with some other methods like: List findAllWhere(Criterion[] where); etc... That works pretty well, but my problem in this case is that you expose an interface with criterions to the service... For me a service is not supposed to import ANY class related to hibernate – Sebastien Lorber Nov 18 '11 at 13:33
  • It's more hard for operations persistence specific like handling flush, clear, caching and anything like that... Like Guillaume said i could expose a cleanUpDatasourceCache for exemple but the problem for me is that the method perhaps doesn't mean anything for a persistence unit with no cache... (but perhaps i could just change the implementation of a no cache persistence unit with an empty method impl?) – Sebastien Lorber Nov 18 '11 at 13:38
  • @SebastienLorber if you check the link , instead of passing criterion[] for any findAllWhere, you can define named queries and pass the arguments as method parameters, you can go through "Hibernate named queries" section and see how Person.findByName is impelmented, you can use annotations as the link is quite old. – dpsdce Nov 18 '11 at 17:10
  • We can also pass arguments and build the criterions inside the dao implementation, but it's not the point. Here i'm talking more about advanced session management – Sebastien Lorber Nov 20 '11 at 15:32
  • @dpsdce What is it `FinderExecutor `? I can't find it now. – Woland Sep 06 '17 at 20:33
1

Hibernate (either as a SessionManager or a JPA EntityManager) is the DAO. The Repository pattern is, as far as I have seen, the best starting place. There is a great image over at the DDD Sample Website which I think speaks volumes about how you keep things things separate.

My application layer has interfaces that are explicit business actions or values. The business rules are in the domain model and things like Hibernate live in the infrastructure. Services are defined at the domain layer as interfaces, and implemented in the infrastructure in my case. This means that for a given Foo domain object (an aggregate root in the DDD terminology) I usually get the Foo from a FooService and the FooService talks to a FooRepository which allows one to find a Foo based on some criteria. That criteria is expressed via method parameters (possibly complex object types) which at the implementation side, for example in a HibernateFooRepository, would be translated in to HQL or Hibernate criterion.

If you need batch processing, it should exist at the application level and use domain services to facilitate this. StartBatchTransaction/EndBatchTransaction. Hibernate may listen to start/end events in order to coordinate purging, loading, whatever.

In the specific case of serializing domain entities, though, I see nothing wrong with taking a set of criteria and iterating over them one at a time (from root entities).

I find that often, in the pursuit of separation, we often try to make things completely general. They are not one in the same - your application has to do something, and that something can and should be expressed rather explicitly.

If you can substitute an InMemoryFooRepository where a HibernateFooRepository was previously being used, you're on the right path. The natural flow through unit and integration testing your objects encourages this when you adhere or at least try to respect the layering outlined in the image I linked above.

Doug Moscrop
  • 4,479
  • 3
  • 26
  • 46
0

I would recommend you let the database handle the export to CSV operation rather than building it yourself in Java, it isn't as efficient. ORM shouldn't really be used for those large scale batch operations, because ORM should only be used to manipulate transactional data.

Large scale Java batch operations should really be done by JDBC directly with transactional support turned off.

However, if you do this regularly, I recommend setting up a reporting database which is a delayed replica of the database that is not used by the application and utilizes database specific replication tools that may come with your database.

Your solution architect should be able to work with the other groups to help set this up for you.

If you really have to do it in the application tier, then using raw JDBC calls may be the better option. With raw JDBC you can perform a query to assemble the data that you require on the database side and fetch the data one row at a time then write to your output stream.

To answer your layers question. Though I don't like using the word layers because it usually implies one thing on top of another. I would rather use the word "components" and I have the following component groups.

application

  1. domain - just annotated JPA classes, no persistence logic, usually a plain JAR file, but I recommend just plop it as a package in the EJB rather than having to deal with class path issues
  2. contracts - WSDL and XSD files that define an interface between different components be it web services or just UI.
  3. transaction scripts - Stateless EJBs that would have a transaction and persistence units injected into them and do the manipulation and persistence of the domain objects. These may implement the interfaces generated by the contracts.
  4. UI - a separate WAR project with EJBs injected into them.

database

  1. O/R diagram - this is the contract that is agreed upon by application and data team to ensure THE MINIMUM that the database will provide. It does not have to show everything.
  2. DDLs - this is the database side implementation of the O/R diagram which will contain everything, but generally no one should care because it implementation details.
  3. batch - batch operations such as export or replicate
  4. reporting - provides queries to get business value reports from the system.

legacy

  1. messaging contracts - these are contracts used by messaging systems such as JMS or WS-Notifications or standard web services.
  2. their implementation
  3. transformation scripts - used to transform one contract to another.
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
-1

It seems to me we need to take another look at the layers. (I hope someone corrects me if I get this wrong.)

  1. Front End/UI
  2. Business
  3. Service/DAO

So for the case of Generating a Report, THe layers break down like so.

  1. Front End/UI
    • will have a UI with a button "Get Some Report"
    • the button will then call the Business layer that knows what the report is about.
    • The data returned by the report generator is given any final formatting before being returned to the user.
  2. Business
    • MyReportGenerator.GenerateReportData() or similar will be called
  3. Service/DAO
    • inside of the report generator DAOs will be used. DAOLocator.GetDAO(Entity.class); or similar factory type methods would be used to get the DAOs. the returned DAOs will extend a Common DAO interface
Raystorm
  • 6,180
  • 4
  • 35
  • 62
-1

Well, to get a clean separation of concern or you can say clean layer separation you can add Service layer to your application, which lies between you FrontEnd and DaoLayer.

You can put your business logic in Service layer and database related things in Dao layer using Hibernate.

So if you need to change something in your business logic, you can edit your service layer without changing the DAO and if you want to change the Dao layer, you can do without changing actual business logic i.e. Service Layer.

gprathour
  • 14,813
  • 5
  • 66
  • 90