Jiew Meng
Jiew Meng

Reputation: 88197

Lazy loading models

Suppose I have an model object Event that contains a Venue. When I query the database to populate the venue, I want to only store the venueid which can be used to lazy load the Venue.

I have data access class Events (plural) with function a function like Event findEventByName(String name). Similar for other model objects. If in my Event class, I have getVenue like:

public Venue getVenue() {
    if (this.venue == null) 
        this.venue = Venues.findVenueById(this.venueid); 
    return this.venue;
}

This will work, but it will couple my model with my data access. I think my design is not really "correct"? So how do I change it. For this academic project I think I won't be using a 3rd party library like Hibernate to get a feel of how this might be implemented.

Upvotes: 0

Views: 325

Answers (1)

BillRobertson42
BillRobertson42

Reputation: 12883

I think you're correct in that its not the best design to couple your object model to you data access.

Modern object to relational mappers are pretty advanced. EclipseLink, for example, can dynamically modify the bytecode of the classfiles to provide for features like lazy loading. They call it weaving.

Upvotes: 1

Related Questions