<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Andy Gibson &#187; Modeling</title>
	<atom:link href="http://www.andygibson.net/blog/tag/modeling/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.andygibson.net/blog</link>
	<description>Open Source Projects &#38; Technical Writings</description>
	<lastBuildDate>Thu, 02 Feb 2012 14:33:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language></language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Deterring &#8220;ToMany&#8221; Relationships in JPA models</title>
		<link>http://www.andygibson.net/blog/article/deterring-tomany-relationships-in-jpa-models/</link>
		<comments>http://www.andygibson.net/blog/article/deterring-tomany-relationships-in-jpa-models/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 13:50:38 +0000</pubDate>
		<dc:creator>Andy Gibson</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Modeling]]></category>

		<guid isPermaLink="false">http://www.andygibson.net/blog/?p=1693</guid>
		<description><![CDATA[This article considers the issues of one to many relationships from the JPA model, and looks at an alternative strategy to provide more efficient and fine grained data access, to build more robust and lightweight applications and web services. A fairly typical use is to have one entity &#8216;owned&#8217; by the other in such a [...]]]></description>
			<content:encoded><![CDATA[<p>This article considers the issues of one to many relationships from the JPA model, and looks at an alternative strategy to provide more efficient and fine grained  data access, to build more robust and lightweight applications and web services.<br />
<span id="more-1693"></span><br />
A fairly typical use is to have one entity &#8216;owned&#8217; by the other in such a way that one entity is said to &#8216;have&#8217; many instances of the other one. A typical example would be customer and orders :</p>
<pre class="brush: java;">
class Customer {

  @OneToMany(mappedBy=&quot;customer&quot;)
  private Set&lt;Order&gt; orders;

}

class Order {
  @ManyToOne
  private Customer customer;
}
</pre>
<p>In this trivial example, the order belongs to a customer, and the customer has a set of orders. We don&#8217;t have a problem with the <code>ManyToOne</code> relationship, especially as it is required in order to map the order back to the customer. When we load an order we will at most get a single reference to a customer.</p>
<p>No, our problem is with the value we get from <code>customer.getOrders()</code> as this set of order entities doesn&#8217;t really serve any useful purpose and can cause more problems than it solves for the following reasons :</p>
<ol>
<li><b>Dumb Relationship</b> &#8211; It will contain every order for this particular customer when you usually only want a subset of the orders that match a set of criteria. You either have to read them all and filter the ones you don&#8217;t want manually (which is what SQL is for) or you end up having to make a call to a method to get the specific entities you are interested in.</li>
<li><b>Unbounded dataset</b> &#8211; How many orders a customer has could vary and you could end up with a customer with thousands of orders. Combined with accidental eager fetching and loading a simple list of 10 people could mean loading thousands of entities.
</li>
<li><b>Unsecured Access</b> &#8211; Sometimes we may want to restrict the items visible to the user based on their security rights. By making it available as a property controlled by JPA we lose that ability or have to implement it further down in the application stack.
<li><b>No Pagination</b> &#8211; Similar to the unbounded dataset, you end up throwing the whole list into the pagination components and letting them sort out what to display. In most cases, you need to treat each dataset like it will eventually contain more than 30 records so you really need to consider pagination early.
</li>
<li><b>Overgrown object graph</b> &#8211; When you request an entity, how much of the object graph do you need? How do you know which pieces to initialize so you can avoid LIEs? This is often the case with JPA, but is also more relevant when you take account of the needs to serialize object graphs to XML or JSON. Sometimes you might need the relationships and sometimes you do not depending on the context you will be using the data in.</li>
<li><b>Rife with pitfalls</b> &#8211; Who saves and cascades what and how do you bind one to the other? You create an order, and assign the customer, do you need to then add it to the customers list of orders or not. What happens if you forget to add it to the customer and you save the customer? Whatever strategy you pick for dealing with this will no doubt end up being implemented inconsistently.
</ol>
<p>(<small>Ok, the first four are really different facets of the same problem, that you can&#8217;t control the data you are getting back.</small>)</p>
<p>So what use are they? Well, they make it really tempting just to use <code>customer.orders</code> in the view which is suitable for some sets of data. They also allow the relationship to be used in ejbql statements, although the inverse of the relationship can also be used in most cases. Specifying this relationship can also allow you to cascade updates/deletes from the customer to the order, but then so can your database.</p>
<h1>Going Granular</h1>
<p>The best alternative I&#8217;ve found is to provide additional methods to obtain the relational information separate from the model. This more granular approach gives you plenty of ways of obtaining data from the database without the dangers and temptations of bad practices. For example, the <code>Order</code> object still has the <code>Customer</code> reference on it, which we use to obtain lists of orders from the data access layer which can be constrained by customer, time frame, or other criteria depending on where it is being used. Also, it allows data to be fetched when needed without having to define a single initialization strategy using annotations or mapping files. The code that knows what pieces of data it needs will have access to facilities to fetch the specific data it needs. Alternatively, the  methods to fetch the data can either be exposed as web services directly or DTO objects can be used to build a data payload to be returned from a single web service that consolidates the calls. Regardless, you don&#8217;t need to worry about setting the JPA fetch or XML/JSON serialization policy permanently in the model.</p>
<p>Some examples might be to fetch orders for a customer in different ways.</p>
<pre class="brush: java;">
public List&lt;Order&gt; getOrders(Long customerId) {...}
public List&lt;Order&gt; getOrders(Long customerId,Date startDate,Date endDate) {...}
public List&lt;Order&gt; getOrders(SearchCriteria searchCriteria,int firstResult,int pageSize) {...}
</pre>
<h1>What about <code>@ManyToMany</code></h1>
<p>Good question. In most cases I find that what starts as a many to many relationship can usually be modeled as a separate entity because when you create a many to many relationship, there is usually additional information stored with that relationship. For example, a Users and Groups <code>ManyToMany</code> relationship has many users belonging to many groups and vice versa. The membership however also probably has start and end dates and also maybe a role within that group. This also exhibits one of the earlier problems in that <code>user.getGroupMemberships()</code> would return all group memberships past and present whereas you probably only want the active ones.  Modeling it as a separate entity means it becomes an entity with two <code>OneToMany</code> relationships.</p>
<p>While there are cases where the many to many relationship is literally just a pair of ids (think blog post tags, many tags to many posts), you could benefit at a later date by using an entity if you decide to add additional information into the relationship.</p>
<p>In summary, moving relational fetches out of the data model and into the data layer means you remove some of the temptations of bad practices and create a library of reusable functions for fetching the data that can be used from different code points.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andygibson.net/blog/article/deterring-tomany-relationships-in-jpa-models/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using Composition in Object Modeling</title>
		<link>http://www.andygibson.net/blog/article/using-composition-in-object-modeling/</link>
		<comments>http://www.andygibson.net/blog/article/using-composition-in-object-modeling/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 14:33:35 +0000</pubDate>
		<dc:creator>Andy Gibson</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Journeyman]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Modeling]]></category>

		<guid isPermaLink="false">http://www.andygibson.net/blog/?p=765</guid>
		<description><![CDATA[Using composition over inheritance is a common design pattern that is often discussed in terms of designing business logic components. However, composition can solve a number of problems in domain object modeling that are created by relying on inheritance to share interface or functionality. Composition is used to delegate implementation in logical units by enlisting [...]]]></description>
			<content:encoded><![CDATA[<p>Using composition over inheritance is a common design pattern that is often discussed in terms of designing business logic components. However, composition can solve a number of problems in domain object modeling that are created by relying on inheritance to share interface or functionality. Composition is used to delegate implementation in logical units by enlisting the help of a reference to an object that implements the required functionality instead of inheriting from it. This reference can be changed to different implementations depending on the needs at the time making for a more flexible design. This same design can be used in domain modeling to overcome some of the problems caused by inheritance. The typical flawed example of using inheritance in object modeling is the <code>Person</code> class which is often subclassed into <code>Employee</code>, <code>User</code> , <code>Customer</code> and <code>Vendor</code> classes.<br />
<span id="more-765"></span></p>
<pre class="brush: java;">
public class Person {

  private Long id;
  private String firstName;
  private String lastName;
}
</pre>
<pre class="brush: java;">
public class User extends Person {

  private String userName;
  private String password;

}
</pre>
<pre class="brush: java;">
public class Customer extends Person {

  private int creditLimit;

}
</pre>
<pre class="brush: java;">
public class Employee extends Person {

  private Date beginDate;
  private Date endDate;

}
</pre>
<p>The classic problems here are that a customer may not always be a person, and a person may be both a customer and a vendor and possibly even be an employee and user. It also ignites the age old debate of whether a user must be an employee or all employees are also users. Using inheritance severely restricts us by taking away our one chance to inherit from a single class and limits our future plans since we are claiming some hard rules such as &#8220;every user is a person&#8221;. </p>
<p>Also, how do we handle a person that leaves the company and comes back? Do we have two different instances of this person in the system? For that matter, most Person instances will be duplicated if a person is a customer and a user and employee. This also leads to problems with things like name changes where one version gets updated but the other doesn&#8217;t.</p>
<h1>Role your own</h1>
<p>One common solution to this problem is to let the <code>Person</code> entity have a collection of roles that they perform so they can be either a user,an employee, a customer or vendor at the same time. This does solve the problems of not having to worry about inheritance hierarchies and whether a user is an employee or vice versa and also lets a person play all those roles at once. However, it only works where you know that every user, customer, vendor or employee will be a person.<br />
It&#8217;s also not very clear how we should reference these entities. If I have an order and need to reference a customer, do I reference the person or the persons customer role? </p>
<h1>Compose Yourself</h1>
<p>Alternatively you can use composition to model the relationship instead of inheritance. We can define a <code>Person</code> class which models just a person and we model Customers and Employees as separate classes that encapsulate the information specific to that role and has a reference to the person associated with it.The benefit here is that the different classes do not have to be subclassed from the <code>Person</code> class which lets us inherit from whatever we want.</p>
<pre class="brush: java;">
public class User {

  private Long id;
  private String userName;
  private String password;
  private Person person;
}
</pre>
<p>and</p>
<pre class="brush: java;">
public class Customer {

  private Long id;
  private Person person;
  private int creditLimit;
}
</pre>
<pre class="brush: java;">
public class Employee {

  private Long id;
  private Person person;
  private Date beginDate;
  private Date endDate;
}
</pre>
<p>We don&#8217;t have to worry about whether <code>Employee</code> should inherit from a <code>User</code> or vice versa since they are entirely different entities but they reference the same person instance. We still have the ability to check for things like users that are employees by finding users and employees that have a matching person reference. Modeling for each of the entities is kept specific to what the entity represents. A customer contains customer information and an employee contains employee information with only a single reference to the person it is representing. </p>
<p>This solves our original problems, but also gives us far more in return. It is a far more flexible design because we can expand the definitions of the classes beyond the hard restriction that every customer must be a person. Let&#8217;s consider a situation where the developers have been asked to allow businesses to also be defined as customers in the system. With the Customer subclassing from Person design it would be a nightmare to change the code to accommodate this. On the other hand, if we designed Customer to use composition, we can easily resolve the problem.</p>
<pre class="brush: java;">
public abstract class AbstractCustomer {

  private Long id;
  private int creditLimit;
  public abstract String getName();
}
</pre>
<p>We can create a person based customer by subclassing it and adding a reference to the person.</p>
<pre class="brush: java;">
public class PersonCustomer extends AbstractCustomer {

  private Person person;

  public abstract String getName() {
    return person.getName();
  }
}
</pre>
<p>For a company based customer, we can again subclass it, this time adding a <code>Company</code> reference. </p>
<pre class="brush: java;">
public class CompanyCustomer extends AbstractCustomer {

  private Company company;

  public abstract String getName() {
    return company.getName();
  }
}
</pre>
<p>Notice that in the abstract base class, we define the <code>getName()</code> method to return the name of the customer. This is made abstract and must be implemented in subclasses specific to the type of customer we are implementing. As a general rule, like any inheritance, common attributes that apply to all types of the entity should go in the base class because then you will be able to call those methods on references to the entity as the base class type instead of having to use a more specific subclass type. For example</p>
<pre class="brush: java;">
  String msg = &quot;Hi There &quot;+customer.getName();
</pre>
<p>is much cleaner than</p>
<pre class="brush: java;">
  String msg = &quot;Hi There &quot;;

  if (customer instanceof PersonCustomer) {
    msg = msg+((PersonCustomer)customer).getPerson.getPersonName();
  }

  if (customer instanceof CompanyCustomer) {
    msg = msg+((CompanyCustomer)customer).getCompany.getCompanyName();
  }
</pre>
<p>Another nice effect of this is modularity since you can add new customer types and the old queries will work the same way. For example, if you start with <code>PersonCustomer</code> you might have a query which lists the customers with a certain credit limit.</p>
<pre class="brush: java;">
select c from Customer c where c.creditLimit &gt; 500
</pre>
<p>If you suddenly add the <code>CompanyCustomer</code> class to the application, the query will still work and will now include company based customers in the list. Remember, the more attributes defined on the base class, the more attributes you will be able access or query on when obtaining instances as the base class. However, note that code driven attributes such as the <code>getName()</code> example above cannot be used for querying in JPA since the attribute does not resolve to a database field. To query by person name you would have to drop down to using the actual class name to query the name fields in the person reference. </p>
<h1>Unique People, non-unique references</h1>
<p>If an employee leaves the company and comes back, we can deal with this much easier because we can set the end date for the existing <code>Employee</code> instance when they leave and when they come back, we can create a new employee instance that references the same <code>Person</code> entity. We still only have one <code>Person</code> instance that is referenced by both <code>Employee</code> instances and thus there is no duplication. This kind of modeling is not only more accurate in that one person was an employee on two different occasions, but it also provides a form of auditing trail for that person. If you store the <code>Employee</code> reference, you can still use either the person or the employee id to search for data. If you wanted to see what orders this specific employee had filled in, you can search using either the employee id which would get the orders for this version of the employee only, or you could match based on the person id and see what orders that person had ever filled in as either employee instance. This also reflects the real world better since there is only ever one person, but they could have multiple periods as an employee. </p>
<h1>Modeling with JPA</h1>
<p>It is fairly easy to model the basic composition in JPA by adding a <code>@ManyToOne</code> annotation to the entity reference (in this case the person). </p>
<p>If you plan on creating subclasses that are backed by different entities (i.e. <code>PersonCustomer</code> and <code>CompanyCustomer</code>) then you have to choose between different inheritance strategies. If there only a few differences between subclasses, you can probably just use a single table. If there are bigger differences between the subclasses, you might want to use a joined table inheritance strategy while keeping the core attributes of the parent class in the main table. </p>
<p>For legacy databases, you could be faced with the problem that the referenced entity id, such as the company or person id in the customer subclass, is stored in the same field in the database record. You can use the same field name for the reference in the subclasses and JPA will work with it.</p>
<pre class="brush: java;">
public class PersonCustomer extends AbstractCustomer {

  @ManyToOne
  @JoinColumn(name=&quot;REF_ID&quot;)
  private Person person;

}

public class CompanyCustomer extends AbstractCustomer {

  @ManyToOne
  @JoinColumn(name=&quot;REF_ID&quot;)
  private Company company;

}
</pre>
<p>Obviously, this method eliminates the opportunity to use foreign keys since a foreign key cannot reference two different tables. New designs should not use this method for this reason even though JPA allows it. If you let JPA drop and create the tables for you, with Hibernate at least, you will end up with a foreign key on the <code>REF_ID</code> field to either the person or company table that will be incorrect when you assign the other subclass type to it. Through luck, it may not raise an error the first time around (if you happen to have an instance of one entity that has the same Id as the other entity type) but it will at some point.</p>
<p>If you really need this design, then you can just build the tables yourself without the foreign key, but you should use a separate field even though it means that each record will have blank field in it. With a separate or join table inheritance strategy, you have no choice but to use separate fields for the references.</p>
<h1>A technique with many uses</h1>
<p>This technique has many applications, but I haven&#8217;t really seen it discussed that much. Obviously, most published application examples (i.e. Pet store or my own Issue Tracker) are trivial or incomplete so they don&#8217;t really require this level of attention to modeling detail. However you start to find all sorts of places this technique can be used for larger domain models either to provide functionality needed now or a flexible design to meet future requirements. Obviously, we don&#8217;t want to use this for everything in case we want to extend any object in our model, but refactoring models once you have gone live can be problematic as it not only involves code refactoring, but database refactoring as well. Adopting this technique early (i.e. pre production) in places it is likely to be needed can save a lot of headaches later on.</p>
<p>As another example, let&#8217;s say you wanted to define a <code>Location</code> which is basically an address and a few other attributes. These locations can be used for all sorts of things in various places in the application. We already have an <code>Address</code> class used throughout our code and we can embed it in our <code>Location</code>.</p>
<pre class="brush: java;">
public class Location {

  @Embedded
  private Address address;

  private Date createdOn;
  private String description;

}
</pre>
<p>Half way through your project you find that since this location class is used everywhere, the needs have changed somewhat and in some places need to have a dynamic address that belongs to a person and needs to be updated when the persons address changes, and some places need a static fixed address that never changes (i.e. delivery address an order was sent to which never changes). </p>
<p>We can easily implement this by creating a <code>Location</code> class that is subclassed into the different implementations that source the address from different places.</p>
<pre class="brush: java;">

public abstract class Location {

  public Address getAddress();
  private String description;
  private Date createdOn;

}

public class FixedLocation extends Location {
  @Embedded
  private Address address;

  public Address getAddress() {
    return address;
  }
}

public class PersonLocation extends Location {

  @ManyToOne
  private Person person;

  public Address getAddress() {
    return person.getPrimaryAddress();
  }

}
</pre>
<p>Not only have we provided a solution for our fixed and dynamic address problem, since <code>FixedLocation</code> will store the address internally while the others will dynamically obtain the latest address from the <code>Person</code>, but we have provided a structure that will allow us to turn any entity into an address provider for our <code>Location</code> class. In some ways, we are using it as more of an adapter pattern since the address sources no longer have a common type. We are using an adapter pattern to make the different implementation sources fit with our <code>Location</code> interface.</p>
<p>I have found a number of uses for such compositions/adapters in places where inheritance would have severely limited my options or provided some difficult challenges,  and by implementing this concept I&#8217;ve been able to make my models far more flexible and be able to accommodate new needs quickly. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.andygibson.net/blog/article/using-composition-in-object-modeling/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

