title: Persisting Units of Measurement in JPA
body: <p>This is in continuation with <a href="https://stackoverflow.com/questions/14252520/is-there-any-available-api-to-represent-various-units-of-item-like-kg-litre-me">this question</a> I asked a yesterday. After going through various resources and consulting people, I was not able to find any JPA annotations supporting API, for mapping units of measurement. So, I decided to go with creating it myself.</p>
  
  <p>Based on various <em>patterns</em> on <em>Observations and Measurements</em> described by <a href="http://martinfowler.com/" rel="nofollow noreferrer">Martin Fowler</a> in his book - <a href="https://rads.stackoverflow.com/amzn/click/com/0201895420" rel="nofollow noreferrer" rel="nofollow noreferrer">Analysis Patterns: Reusable Object Models</a>, I tried to create a basic implementation to meet my needs. I've created two entities, <code>Unit</code> and <code>Quantity</code>, as below: -</p>
  
  <h2>Unit entity: -</h2>
  
  <pre><code>@Entity
  @Table(name = "unit")
  public class Unit {
  
      @Id
      @Column(name = "symbol")
      private String symbol;
  
      @Column(name = "name")
      private String name;
  
      @Column(name = "multiplier")
      private Number multiplier;
  
      @ManyToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = "symbol")
      private Unit baseUnit;
  
      public Unit() {
  
      }
  
      public Unit(String symbol, String name, Number multiplier, Unit baseUnit) {
          this.symbol = symbol;
          this.name = name;
          this.multiplier = multiplier;
          this.baseUnit = baseUnit;
      }
  
          /** Getters and Setters **/
  }
  </code></pre>
  
  <h2>Quantity Entity: -</h2>
  
  <pre><code>@Entity
  @Table(name = "quantity")
  public class Quantity {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int quantityId;
  
      private Number amount;
  
      @ManyToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = "unit")
      private Unit unit;
  
      public Quantity() {
  
      }
  
      public Quantity(Number amount, Unit unit) {
          this.amount = amount;
          this.unit = unit;
      }
  
      /** Getters and Setters **/
  }
  </code></pre>
  
  <p>And in <code>Item</code> entity, I'm having a reference of <code>Quantity</code> entity, to represent my <code>unitOfIssue</code> and <code>unitOfPurchase</code>. And then, wherever I've to deal with some quantity, I'll have this <code>Quantity</code> reference. </p>
  
  <p>Now, here's the problem I'm facing now. Since <code>Quantity</code> class here will be an <code>entity</code>. So, we have to map it to a <em>DB Table</em>. So, the caveat here is, everytime I want to add some quantity, or update the quantity, an entry will go in the <code>Quantity</code> table first, and then it will be updated in the <code>Item</code> table. I think, the problem is pretty clear. We would have huge entry in the <code>Quantity</code> table, which clearly shows a bad design.</p>
  
  <p>Can someone give an insight on the approach and what options I've while I implement this pattern in JPA? And how can I solve the problem?</p>