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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:52:29+00:00 2026-06-15T04:52:29+00:00

I am having a problem with mapping a entity that contains a list (or

  • 0

I am having a problem with mapping a entity that contains a list (or map). I’ve tried various combinations of annotations but the two variations that work the ‘best’ both have this behavior. If the parent-object has, for example, 3 child objects, the parent object is retrieved three times.

My Code

In my case i have an UserOrder object that contains pairs of Products with a count. The datastructure looks like this

@Entity
public class UserOrder{ 
    /* 
     * VARIATION 1 
     * I did actually vary some of these annotions, not all are necessary. 
     * I believe the @CollectionTable is the key here
     */
    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable
    @Column
    @Embedded
    private List<ProductCount> products;

    /*
     * VARIATION 2
     */
    @ElementCollection(fetch = FetchType.EAGER)
    private Map<Product, Integer> products = new HashMap<Product, Integer>();

    // get/set
    // ...
}

@Embeddable
/* Only used in variation 1 */
public class ProductCount{
    @OneToOne
    private Product product;

    @Column
    private Integer amount;

    // get/set
}

@Entity
public class Product
{
    // ...
}

Then this is called and displayed using something like this (this is for variation 2)

<!-- Called via DAO/Service layers
List<Order> orders = getSession().createCriteria(UserOrder.class).list();
-->
<c:forEach items="${orders}" var="order">
    <fmt:formatDate value="${order.time}" type="both" />
    <c:forEach items="${order.products}" var="pc">
        ${pc.value} x ${pc.key.name}
    </c:forEach>
</c:forEach>

For the record, I am using Spring (3.1), Hibernate (3.5) and I’d like to stay with JPA annotations.

The Problem

When I retrieve the orders, I get a UserOrder-object for each of the products it contains, instead of one UserOrder for each order I have stored. So I get duplicates in my list, all with complete information of the products it contains. Orders without products are also included

  • Order 1 — Has 2 products
    • 1 x DogToy1
    • 3 x Cat Toy 1
  • Order 1 — Has 2 products
    • 1 x DogToy1
    • 3 x Cat Toy 1
  • Order 2 — Has 1 product
    • 3 x Cat Toy 1
  • Order 3 — Has 0 products

How do I make Hibernate return me one (1) UserOrder instead of one-for-each-product? Or, have I even taking the right approach to modelling this structure?

  • 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-15T04:52:31+00:00Added an answer on June 15, 2026 at 4:52 am

    Options:

    • lazy load the child collections (there should be an option to batch loading lazy loaded collections)
    • use DistinctRootentityResulttransformer on the query
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with the knockout-mapping plugin with IE8. Our situation is that
I'm having trouble mapping two entities together with Entity Framework CTP5 using Code First
I'm having a problem in mapping an existing data class member to the database..
Having odd problem with mapping tables with code-first EF. I have a parent class
I'm having a problem where Sharp Architecture will correctly map everything I have setup
Maybe im just an idiot, but I am having serious issues mapping relationships with
I'm having a problem getting FluentNHibernate's HasMany<> mapping to behave as I would expect.
i am having no problem at all compiling/debugging my web app, but when i
I am having a problem mapping. I was reading scottGU post of data shaping
I'm having a problem with my mapping. I can't get it to work. I

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.