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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:06:18+00:00 2026-05-25T20:06:18+00:00

I have recently started playing around with the Play! Framework for Java, version 1.2.3

  • 0

I have recently started playing around with the Play! Framework for Java, version 1.2.3 (the latest). While testing out the framework, I came across a strange problem when trying to persist a Map object inside a Hibernate entity called FooSystem. The Map object maps a long to a Hibernate entity I have called Foo, with the declaration Map<Long, Foo> fooMap;

My problem is as follows: The correct tables are created as I have annotated them. However, when the FooSystem object fs is persisted, the data in fs.fooMap is not!

Here is the code I am using for the entities. First is Foo:

package models.test;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import play.db.jpa.Model;

@Entity
public class Foo extends Model
{
    @ManyToOne
    private FooSystem foosystem;

    public Foo(FooSystem foosystem)
    {
        this.foosystem = foosystem;
    }
}

And here is FooSystem:

package models.test;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import play.db.jpa.Model;

@Entity
public class FooSystem extends Model
{
    @ManyToMany(cascade = {CascadeType.ALL, CascadeType.PERSIST})
    @JoinTable(
        name = "fooMap",
        joinColumns = @JoinColumn(name = "foosystem"),
        inverseJoinColumns = @JoinColumn(name = "foo")
    )
    private Map<Long, Foo> fooMap = new HashMap<Long, Foo>();

    public FooSystem()
    {
        Foo f1 = new Foo(this);
        Foo f2 = new Foo(this);
        fooMap.put(f1.getId(), f1);
        fooMap.put(f2.getId(), f2);
    }

    public Map<Long, Foo> getFooMap()
    {
        return fooMap;
    }
}

Here is the Controller class I am using to test my set-up:

package controllers;

import javax.persistence.EntityManager;
import models.test.FooSystem;
import play.db.jpa.JPA;
import play.mvc.Controller;

public class TestController extends Controller
{
    public static void index() {
        EntityManager em = JPA.em();
        FooSystem fs = new FooSystem();
        em.persist(fs);
        render();
    }
}

The Play! framework automatically created a transaction for the HTTP request. Although data is inserted into the foo and foosystem tables, nothing is ever inserted into the foomap table, which is the desired result. What can I do about this? What am I missing?

  • 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-05-25T20:06:19+00:00Added an answer on May 25, 2026 at 8:06 pm

    I managed to solve this problem using the advice of Java Ka Baby. The issue was actually not in my Model classes; the problem lay within the Controller. Specifically, I was saving the entities in the wrong order. Once I realized that using the @ElementCollection annotation on the Map<Long, Foo> produced the same effects as the join table I was manually specifying, I tried I thought experiment where I re-thought how I was saving my entities.

    In the code I posted above, you can see in the FooSystem constructor that two Foo objects, f1 and f2, are put into fooMap before the Foo objects are persisted. I realized that if f1 is not in the database when it is put into the map, how is JPA able to use its ID as a foreign key in the join table?

    If you can see where I’m going with this line of reasoning, you can see that the obvious answer is that JPA is not able to accomplish this amazing feat of using a foreign key to reference a nonexistent key. The bizarre thing is that the Play! console did not note any errors at all for the original code I posted, even though it was not correct at all. Either the framework swallowed every Exception thrown during the process, or I’ve written code that should produce an Exception.

    So to fix the problem, I persisted the Foo entities before any operations were performed on them. Only then did I put them into fooMap. Finally, once fooMap was populated, I persisted the FooSystem entity.

    Here is the corrected TestController class:

    package controllers;
    
    import javax.persistence.EntityManager;
    import models.test.Foo;
    import models.test.FooSystem;
    import play.db.jpa.JPA;
    import play.mvc.Controller;
    
    public class TestController extends Controller
    {
        public static void index() {
            EntityManager em = JPA.em();
            FooSystem fs = new FooSystem();
            Foo f1 = new Foo(fs);
            Foo f2 = new Foo(fs);
            f1.save();
            f2.save();
            fs.put(f1.getId(), f1);
            fs.put(f2.getId(), f2);
            fs.save();
            render();
        }
    }
    

    And, since I changed FooSystem, here is the final code for that class:

    package models.test;
    
    import java.util.HashMap;
    import java.util.Map;
    import javax.persistence.ElementCollection;
    import javax.persistence.Entity;
    import play.db.jpa.Model;
    
    @Entity
    public class FooSystem extends Model
    {
        @ElementCollection
        private Map<Long, Foo> fooMap = new HashMap<Long, Foo>();
    
        public FooSystem()
        {
        }
    
        public Map<Long, Foo> getFooMap()
        {
            return fooMap;
        }
    
        public void put(Long l, Foo f)
        {
            fooMap.put(l, f);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have recently started to see this exception while running the play framework in
I have recently started playing around with iOS development and have got most of
I have just started playing around with using Dojo in Zend Framework and things
Recently I have started playing with jQuery, and have been following a couple of
I have recently started learning Perl and one of my latest assignments involves searching
I have recently started seeing user agents like Java/1.6.0_14 (and variations) on my site
I've recently started playing around with ASP.NET and I was just about to connect
I have recently started playing with databases, trying to teach myself using examples. I
I've recently started playing around with the ASP.NET MVC NerdDinner sample, and as part
I recently just started playing around in PHP and got myself a small project/homework.

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.