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 9046029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:31:52+00:00 2026-06-16T11:31:52+00:00

I’m new to Hibernate. I’m using Hibernate 4.0 and specifically JPA annotations. I have

  • 0

I’m new to Hibernate. I’m using Hibernate 4.0 and specifically JPA annotations.

I have an Order class. An order can contain multiple Skus (‘sku’ is a stock keeping unit – see Wikipedia) and one or more of each sku. The Order and Sku classes are below. I’d like to persist the order together with its associated skus in the same table but am open to using two tables if needed. I can’t work out how to persist the HashMap – the runtime exception thrown states javax.persistence.PersistenceException: org.hibernate.exception.DataException: Data truncation: Data too long for column 'skusInOrder' at row 1.

I have read [this][2] Stackoverflow question: – does the advice to use the @Lob annotation make sense in my scenario? Do I need to use @Embedded or @Embeddable and/or @ElementCollection?

package com.newco.drinks.data;

import java.io.Serializable;
import java.math.RoundingMode;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.money.CurrencyUnit;
import org.joda.money.Money;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="Orders")
public class Order implements Serializable {

    private static final long serialVersionUID = 5915005362337363656L;
    private List<Sku> availableSkus;
    private String barId;
    private java.util.Date collectionNotificationTime;
    private CurrencyUnit currencyUnit;
    private String customerId;
    private String orderId;
    private Money orderTotal;
    private String paymentProcessorRef; 
    private Boolean paymentSuccessful;
    private HashMap<Sku, Integer> skusInOrder = new HashMap<Sku, Integer>(); // skuId and quantity thereof
    private java.util.Date timeReceived;

    /**
     * 
     * @param sku
     * @param quantity
     * @throws IllegalArgumentException
     *             if quantity is zero or negative, or if the Order does not
     *             contain the Sku
     * @throws ConcurrentModificationException
     */
    void add(Sku sku, int quantity) throws IllegalArgumentException, ConcurrentModificationException {

        if (quantity <= 0) {
            throw new IllegalArgumentException("Quantity must be greater than zero, but was " + quantity);
        }

        if (skusInOrder.isEmpty() || !skusInOrder.containsKey(sku)) {
            skusInOrder.put(sku, quantity);
        } else {
            if (skusInOrder.containsKey(sku)) {
                int i = skusInOrder.get(sku);
                skusInOrder.put(sku, i + quantity);
            } else {
                throw new IllegalArgumentException("Order " + getOrderId() + " does not contain SKU " + sku.getSkuId());
            }
        }
    }

    private Money calculateOrderTotal() {

        int decimalPlaces = currencyUnit.getDecimalPlaces();
        String zeroString = null;

        switch (decimalPlaces) {
        case 1:
            zeroString = "0.0";
            break;
        case 2:
            zeroString = "0.00";
            break;
        case 3:
            zeroString = "0.000";
            break;
        case 4:
            zeroString = "0.0000";
            break;
        }

        Money total = Money.of(currencyUnit, new Double(zeroString));

        for (Sku sku : skusInOrder.keySet()) {

            int quantity = skusInOrder.get(sku);
            Money totalExTax = sku.getPrice().multipliedBy(quantity, RoundingMode.HALF_UP);

            double taxRate = sku.getTaxRate();
            if (taxRate > 0) {
                Money tax = totalExTax.multipliedBy(taxRate / 100, RoundingMode.HALF_UP);
                total = total.plus(totalExTax.plus(tax));
            } else {
                total = total.plus(totalExTax);
            }
        }

        return total;
    }

    /**
     * 
     * @return a List of Sku objects available to be added to this order.
     *         Different bars will have different Skus available.
     */
    @Transient
    @OneToMany
    public List<Sku> getAvailableSkus() {
        return availableSkus;
    }

    @Column(name="barid", nullable=false)
    public String getBarId() {
        return barId;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "collectionnotificationtime")
    public java.util.Date getCollectionNotificationTime() {
        return collectionNotificationTime;
    }

    @Column(name="currencyunit", nullable=false)
    public CurrencyUnit getCurrencyUnit() {
        return currencyUnit;
    }

    @Column(name="customerid", nullable=false)
    public String getCustomerId() {
        return customerId;
    }

    @Id
    @Column(name="orderid", nullable=false)
    public String getOrderId() {
        return orderId;
    }

    //@Column(name="ordertotal", nullable=false)
    @Transient
    public Money getOrderTotal() {
        return calculateOrderTotal();
    }

    @Column(name="paymentprocessorref", nullable=true)
    public String getPaymentProcessorRef() {
        return paymentProcessorRef;
    }

    @Column(name="paymentsuccess", nullable=true)
    public Boolean getPaymentSuccessful() {
        return paymentSuccessful;
    }

    @Column(name="skusinorder", nullable=false)
    public HashMap<Sku, Integer> getSkusInOrder() {
        return skusInOrder;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "timereceived")
    public java.util.Date getTimeReceived() {
        return timeReceived;
    }

    /**
     * 
     * @param sku
     * @param quantity
     * @throws IllegalArgumentException
     *             if quantity is zero or negative, or if the Sku does not form
     *             part of the Order
     * @throws ConcurrentModificationException
     */
    void remove(Sku sku, int quantity) throws IllegalArgumentException, ConcurrentModificationException {

        if (quantity <= 0) {
            throw new IllegalArgumentException("Quantity to remove must be greater than zero for order " + getOrderId() + ", but was " + quantity);
        }

        if (skusInOrder.isEmpty() || !skusInOrder.containsKey(sku)) {
            throw new IllegalArgumentException("Cannot remove sku " + sku.getSkuId() + " which doesn't form part of order " + getOrderId());
        } else {
            int i = skusInOrder.get(sku);
            if (quantity <= i) { // fine, this is expected
                skusInOrder.put(sku, i - quantity);
            } else if (quantity == i) { //okay, remove that sku altogether as its quantity is now zero
                skusInOrder.remove(sku);
            }
        }
    }

    void setAvailableSkus(List<Sku> availableSkus) {
        this.availableSkus = availableSkus;
    }

    void setBarId(String barId) {
        this.barId = barId;
    }

    void setCollectionNotificationTime(
            java.util.Date collectionNotificationTime) {
        this.collectionNotificationTime = collectionNotificationTime;
    }

    void setCollectionReadySent(java.util.Date collectionReadySent) {
        this.collectionNotificationTime = collectionReadySent;
    }

    void setCurrencyUnit(CurrencyUnit currencyUnit) {
        this.currencyUnit = currencyUnit;
    }

    void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    void setOrderTotal(){
        orderTotal = calculateOrderTotal();
    }

    void setPaymentProcessorRef(String paymentProcessorRef) {
        this.paymentProcessorRef = paymentProcessorRef;
    }

    void setPaymentSuccessful(Boolean paymentSuccessful) {
        this.paymentSuccessful = paymentSuccessful;
    }
}


package com.newco.drinks.data;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.joda.money.CurrencyUnit;
import org.joda.money.Money;

@Entity
@Table( name = "Skus" )
public class Sku implements Serializable{

    private static final long serialVersionUID = 8375466982619713795L;
    private String skuId;
    private String barId;
    private CurrencyUnit currencyUnit;
    private double price;
    private String description;
    private String brand;
    private String skuType;
    private double taxRate;

    protected Sku(){
        // no args constructor for Hibernate use
    }

    public Sku(String skuId, String barId, CurrencyUnit currencyUnit, double price, String description, String brand, String skuType, double tax) {
        setSkuId(skuId);
        setBarId(barId);
        setCurrencyUnit(currencyUnit);
        setPrice(price);
        setDescription(description);
        setBrand(brand);
        setSkuType(skuType);
        setTaxRate(tax);
    }

    @Column(name="barid", nullable=false)
    public String getBarId() {
        return barId;
    }

    @Column(name="brand", nullable=true)
    public String getBrand() {
        return brand;
    }

    @Column(name="currencyunit", nullable=false)
    public CurrencyUnit getCurrencyUnit() {
        return currencyUnit;
    }

    @Column(name="description", nullable=false)
    public String getDescription() {
        return description;
    }

    @Column(name="price", nullable=false)
    public Money getPrice() {
        return Money.of(currencyUnit, price);
    }

    @Id
    @Column(name="skuid", nullable=false)
    public String getSkuId() {
        return skuId;
    }

    @Column(name="skutype", nullable=false)
    public String getSkuType() {
        return skuType;
    }

    @Column(name="taxrate", nullable=false)
    public double getTaxRate() {
        return taxRate;
    }

    void setBarId(String barId) {
        this.barId = barId;
    }

    void setBrand(String brand) {
        this.brand = brand;
    }

    void setCurrencyUnit(CurrencyUnit currencyUnit) {
        this.currencyUnit = currencyUnit;
    }

    void setDescription(String description) {
        this.description = description;
    }

    void setPrice(double price) {
        this.price = price;
    }

    void setSkuId(String skuId) {
        this.skuId = skuId;
    }

    void setSkuType(String skuType) {
        this.skuType = skuType;
    }

    void setTaxRate(double tax) {
        this.taxRate = tax;
    }



}

[2]: How to persist a HashMap with hibernate, Integer

  • 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-16T11:31:56+00:00Added an answer on June 16, 2026 at 11:31 am

    First, define your field to be of type Map rather than HashMap (always prefer interfaces to concrete classes for referencing objects).

    Then the best option is to use @ElementCollection. That way hibernate will create (if hbm2ddl is enabled) a separate table and fill the results there. If you use a @Lob it will still work, but the field will be a “black-box” when read outside of java (i.e. a command-line mysql tool)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.