3

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).

ObjectDB
  • 1,294
  • 7
  • 8
jonny bordo
  • 519
  • 3
  • 6
  • 17

2 Answers2

5

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;
jonny bordo
  • 519
  • 3
  • 6
  • 17
3

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.

jeffrey_t_b
  • 1,772
  • 12
  • 17
  • Thanks, I really got those two mixed up. I knew I was writing JPA, but since I got a JDO exception, I thought it was something that eclipse might have done in the background. I also read the google appengine tutorials on jpa but it didn't help. They don't write about the notations too much. And some of the links there are broken. Anyhow your reply made me think I should search for why I got a JDO exception when I was writing in JPA. And FINALLY I found the answer, so thanks! – jonny bordo Aug 13 '11 at 19:45