Question

I'm pretty new to JPA/JDO and the whole objectdb world.

I have an entity with a set of strings, looks a bit like:

@Entity
public class Foo{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Key id;

    private Set<String> bars;

    public void setBars(Set<String> newBars){
        if(this.bars == null)
            this.bars = new HashSet<String>;
        this.bars = newBars;
    }

    public Set<String> getBars(){
        return this.bars;
    }

    public void addBar(String bar){
        if(this.bars == null)
            this.bars = new HashSet<String>;
        this.bars.add(bar);
    }

}

Now, in another part of the code, I'm trying to do something like this:

EntityManager em = EMF.get().createEntityManager();
Foo myFoo = em.find(Foo.class, fooKey);
em.getTransaction().begin();
myFoo.addBar(newBar);
em.merge(myFoo);
em.getTransaction().commit();

When, of course, newBar is a String.

But, what I get is:

javax.jdo.JDODetachedFieldAccessException: You have just attempted to access field      "bars" yet this field was not detached when you detached the object. Either dont access this field, or detach it when detaching the object.

I've searched for an answer, but I couldn't find one.

I've seen someone ask about a Set of strings, and he was told to add an @ElementCollection notation.

I tried that, but I got an error about the String class Metadata (I don't really understand what it means.)

I would really appreciate some help on this thing, even a good reference to someone explaining this (in simple English).

Was it helpful?

Solution

OK, So I found the answer in some blog.

So for anyone who's interested:

In order to use a Collection of simple data types (in JPA), a @Basic notation should be added to the collection. So from my example at the top, It should've been written:

@Basic
private Set<String> bars;

OTHER TIPS

So you are using JPA, right? (I see EntityManager rather than JDO's PersistenceManager.) Since you are getting a JDO error, I suspect that your app isn't configured properly for JPA.

JPA docs: http://code.google.com/appengine/docs/java/datastore/jpa/overview.html

JDO docs: http://code.google.com/appengine/docs/java/datastore/jdo/overview.html

You need to pick one datastore wrapper and stick with it. The default new app with the Eclipse tools is configured for JDO, and it is a reasonable choice, but you'll have to change your annotations around a little bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top