1

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?

Engineert
9291 gold badge5 silver badges18 bronze badges
asked May 15, 2019 at 10:15
1
  • 1
    App Layers + ORMs + Lazy Loading = Pain. Commented May 15, 2019 at 19:11

2 Answers 2

1

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);
 }
}
answered May 15, 2019 at 15:08
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.

answered May 15, 2019 at 10:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.