NOTE: The one-has-one relationship described here is the unidirectional object relationship. It is not to be confused with the bidirectional one-to-one database relationship between two tables.
Let's say you have a Post object that contains an User object in an unidirectional one-has-one relationship. The classes are illustrated below:
public class Post { private int id; private String title; private User user; public Post() { // mandatory } public Post(int id) { this.id = id; } public Post(String title, User user) { this.title = title; this.user = user; } // getters and setters }
public class User { private int id; private String username; public User() { // mandatory } public User(int id) { this.id = id; } public User(String username) { this.username = username; } // getters and setters }
The way this works in the database is that the Posts table will have a user_id column to store the user for the post. Below we have the tables structures:
create table Posts(
id integer primary key auto_increment,
title varchar(200),
user_id integer
);
create table Users(
id integer primary key auto_increment,
username varchar(25)
);
NOTE: Although not a requirement you can make the user_id in the Posts table a foreign key related to the primary key of the Users table. This is done in the database and requires no configuration in the Java side.
Now to indicate this relationship when you do the mapping, all you have to do is use a nested property like the example below:
BeanConfig postConfig = new BeanConfig(Post.class, "Posts"); postConfig.pk("id", DBTypes.AUTOINCREMENT); postConfig.field("title", DBTypes.STRING); postConfig.field("user.id", "user_id", DBTypes.INTEGER); // "user_id" is the column name in the Posts table
With the property name "user.id" we are indicating that the "id" will be stored inside the "user" property of the Post bean, in other words, it is a property of a property or a nested property. Note that your Post object must have a "user" getter (getUser() method) and that the User object must have an "id" setter (setId(int id) method). When you load a Post object, MentaBean will automatically instantiate the User object and fill its "id" with the value coming from the "user_id" column.
Inserts and Updates work as you would expect: the "user_id" field is taken from the User object inside the Post object.
According to Martin Fowler, Lazy Load is "An object that doesn't contain all of the data you need but knows how to get it". MentaBean follows this philosophy and doesn't make any load behind your back, however it provides support for getting all dependencies manually whenever you want. So, when the Post object is loaded, an User object is instantiated and injected in the Post object however this User object will only have the "id" property set. Whenever you decide that the complete User dependency is needed you can easily load it with the line of code below:
Post post = new Post(3); // loading the Post object session.load(post); // ok, now we need to load the User of the Post session.load(post.getUser());
So to conclude: By a conscious choice, MentaBean never loads the full object graph (eager loading). It always use lazy-loading however the dependencies are never loaded automagically. You decide when you need the dependencies and load them by invoking one line of code.