Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9126653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:05:26+00:00 2026-06-17T07:05:26+00:00

This is in continuation with this question I asked a yesterday. After going through

  • 0

This is in continuation with this question 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.

Based on various patterns on Observations and Measurements described by Martin Fowler in his book – Analysis Patterns: Reusable Object Models, I tried to create a basic implementation to meet my needs. I’ve created two entities, Unit and Quantity, as below: –

Unit entity: –

@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 **/
}

Quantity Entity: –

@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 **/
}

And in Item entity, I’m having a reference of Quantity entity, to represent my unitOfIssue and unitOfPurchase. And then, wherever I’ve to deal with some quantity, I’ll have this Quantity reference.

Now, here’s the problem I’m facing now. Since Quantity class here will be an entity. So, we have to map it to a DB Table. So, the caveat here is, everytime I want to add some quantity, or update the quantity, an entry will go in the Quantity table first, and then it will be updated in the Item table. I think, the problem is pretty clear. We would have huge entry in the Quantity table, which clearly shows a bad design.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:05:27+00:00Added an answer on June 17, 2026 at 7:05 am

    I would recommend this instead. Since units of measure are unlikely to change often, it is worth building them into the code. That way you can meaningfully use the values in the program itself.

    It also makes the database more cohesive if it will ever be used anywhere else. You can also extend Quantity to be things LinearQuantity, ArealQuantity, VolumetricQuantity (etc.) to make sure someone isn’t trying to buy 30 feet of oil.

    @Embeddable
    public class Quantity{
    
       public enum Unit {FEET,METERS,INCHES,MM}
    
       @Enumerated( value = EnumType.STRING)
       private Unit unit;
    
       private Number amount;
    
       public Quantity() {
    
       }
    }
    
    
    @Entity
    Public Class PurchaseOrder
    {
        @Embedded
        @AttributeOverrides({
          @AttributeOverride(name="unit", column=@Column(name="UNIT")),
          @AttributeOverride(name="amount", column=@Column(name="AMOUNT"))
        })
        private Quantity quantity;
        ....
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a continuation of my previous question: Could not find an implementation of
This is a continuation of a question I asked yesterday, where I asked how
And my confusion with JSF continues. This is a continuation of a question asked
I guess this is a continuation of the last question I asked: bulk insert
This is a continuation of this question from yesterday . Here are my three
this is a continuation of question I asked a while back. Create a typemap
This question is in continuation to my previous question, in which I asked about
This is continuation of my previous question Android: Music Player gets started itself after
This is continuation to the question I already asked a while back. I've been
So this is a continuation of a question I asked earlier today. I've built

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.