The Builder Pattern Howto Use Itwith Hibernate
The Builder Pattern Howto Use Itwith Hibernate
www.thoughts-on-java.org
The Builder Pattern – How to use it with Hibernate
Creating Builders for a Graph of Entities
A meaningful builder API needs to help you to create a graph of entity
objects. For the example in this article, that means that you not only
need to provide a builder for the Order and the OrderItem entity. You
also need to support the creation of a Set of OrderItem objects for a
given Order.
OK, let’s take a look at the code of the builder classes that I use in the
code snippet.
The OrderBuilder
The Order entity is the root of the small graph of entities. When you
create an Order object, you can ignore the primary key and the version
attribute. These are generated attributes that you don’t need to
provide when you instantiate a new object.
When you ignore these attributes, there are only 2 attributes left:
www.thoughts-on-java.org
The Builder Pattern – How to use it with Hibernate
Due to that, you only need to provide a withOrderDate(LocalDate
orderDate), a withItems(Set<OrderItem>items) and a buildOrder()
method to be able to build an Order entity object.
They also enable you to hide technical details. I use that in the
buildOrder method, to hide the double linking between the Order and
OrderItem objects that’s required to manage the bidirectional one-
to-many association.
this.orderDate = orderDate;
return this;
}
www.thoughts-on-java.org
The Builder Pattern – How to use it with Hibernate
// Create Set<OrderItem> and link with order
Set<OrderItem> items = this.itemListBuilder.items;
for (OrderItem item : items) {
item.setOrder(o);
}
o.setItems(items);
return o;
}
}
The OrderItemListBuilder
The OrderItemListBuider class bridges the gap between the Order and
the OrderItemBuilder by managing the Set of OrderItems.
www.thoughts-on-java.org
The Builder Pattern – How to use it with Hibernate
public OrderBuilder buildItemList() {
return this.orderBuilder;
}
}
The OrderItemBuilder
The OrderItemBuilder implements the required methods to build an
OrderItem.
public OrderItemBuilder() {
super();
}
www.thoughts-on-java.org
The Builder Pattern – How to use it with Hibernate
return this;
}
www.thoughts-on-java.org
The Builder Pattern – How to use it with Hibernate
Then I want to add OrderItems to the Order. To do that, I first call the
withItems() method. It returns an OrderItemListBuilder on which I call
the addItem() method to get an OrderItemBuilder that’s linked to the
OrderItemListBuilder. After I’ve set the reference to the Product
entity and the quantity that the customer wants to order, I call the
addToList() method. That method builds an OrderItem object with the
provided information and adds it to the Set<OrderItem> managed by
the OrderItemListBuilder. The method also returns the
OrderItemListBuilder object. That allows me to either add another
OrderItem to the Set or to call the buildItemList() to complete the
creation of the Set.
Conclusion
You can easily apply the builder pattern to your entities. In contrast to
the fluent interface pattern, you don’t need to work around any
technical requirements defined by the JPA specification or the
Hibernate documentation.
www.thoughts-on-java.org