ActiveJDBC – as ORM :: More Examples
ActiveJDBC More Examples |
Some more concepts from ActiveJDBC
ActiveJDBC is lazy by default.
In this sense, it has semantics closer to ActiveRecord than Hibernate.
This means that if you have a model Passenger and a model Address,and they have a one to many relationship, when a passenger has many addresses, the code: does not load the associated addresses.
Passenger p = Passenger.findById(1);
Let’s consider an example where an ORM could unexpectedly generate a huge number of inefficient queries:
List<Address> addresses = Address.findAll();
for(Address address: addresses){
Passenger p = address.parent(Passenger.class);
System.out.println(p);
}
In the above example, the number of queries generated and executed is going to be N + 1, were N is a number of addresses.
This is because the first query is to get all addresses, and then for each address, there is a new query to get a user parent (line 4).
This approach is going to kill performance in some applications.
A better approach is to load all parents at once by a single query:
List<Address> addresses = Address.findAll().include(Passenger.class);
for(Address address: addresses){
Passenger p = address.parent(Passenger.class);
System.out.println(p);
}
The ActiveJDBC will then issue two queries:
one to get all Address(es) and the other to get all corresponding User(s).
The same logic can be applied to all relationships going up and down: one to many, many to one and many to many.
Eager simultaneous loading of parents and children
Suppose we have two one to many relationships:
Author has many Posts and a Post has many Comments.
In cases like these, we can load a post and all corresponding Authors and Comments very efficiently:
List<Post> todayPosts = Post.where("post_date = ?", today).include(Author.class, Comment.class);
Downloads Example |
Click the below Link to download complete Library and Examples
- ActiveJDBC Mapping Example using Ant and Eclipse(7 KB)
- ActiveJDBC Lazy Loading Example using Ant and Eclipse(7 KB)
- ActiveJDBC Auto Generated Field Example using Ant and Eclipse(7 KB)
- ActiveJDBC Eager Loading Example using Ant and Eclipse(7 KB)
- ActiveJDBC Batch Operation Example using Ant and Eclipse(7 KB)
- ActiveJDBC Transaction Example using Ant and Eclipse(7 KB)
- ActiveJDBC Convert Example using Ant and Eclipse(7 KB)
- ActiveJDBC with Web Application using Ant and Eclipse(7 KB)
- ActiveJDBC Limit and Sort Example using Ant and Eclipse(7 KB)
- First ActiveJDBC Basic Example using Ant and Eclipse(7 KB)
- All required JAR library (1.4 MB)
Recent Comments