2

In a new Java project I plan to use an extra layer above Hibernate entities. So there would be a CustomerEntity and CustomerDomain. The reason for that is that the CustomerDomain can contain extra data fields from other sources like SOA or other DB. (Maybe DTO is better here than Domain.)

In the pilot solution both the entity class and the domain class has a constructor the copies common fields. Like :

public CustomerDomain(CustomerEntity entity)

In case of complex objects, this solution reads all data from the entity that makes Lazy loading ineffective.

So my question is how to separate database entities from business entities keeping the lazy loading benefits?

  • 1
    App Layers + ORMs + Lazy Loading = Pain. – Graham May 15 at 19:11
0

I think, what you need is Projection Queries.

You may need extra information beyond CustomerEntity, such as UserName from UserEntity, OrderCount from Orders...

Projection Queries let you load custom data by producing SQL query from your code. When you call proper method to load records to memory( ToList(), ToArray(), AsEnumarable() in C#), this SQL query will execute and return your data.

0

One easy solution is to not create your Customer domain with data from the database, instead have it created with the id for example, and have it expose the actual business-behavior instead data.

This way data will be loaded exactly when you need it and can be optimized for the use-case at hand.

For example:

public final class Customer {
    ...
    private final long id;

    public Customer(long id, ...) {
        this.id = id;
        ...
    }

    // Whatever, I don't know your domain
    public void freezeCreditCards() {
        sql("update creditcard set active=0 where customer = {}", id);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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