Hello.
I'm using JPA for the App Engine datastore.
And I've got some problems.

I've got an entity <Container> than contains entities <Item>(s).
Here is my actions.
I start transaction then update entity then flush updates to database.
Then I get all entities of class Item(s) for a container which
contains my updated Item.
But the list of retrieved items contains Item that does not have my
changes.
What is wrong with my code???


I've modelled this problem. (see code)

@Entity
public class Container implements Serializable {

       private static final long serialVersionUID = 1L;

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

       @OneToMany(mappedBy = "container", fetch = FetchType.LAZY)
       private List<Item> items = new ArrayList<Item>();

       public void setKey(Key key) {
               this.key = key;
       }

       public Key getKey() {
               return key;
       }

       public void setItems(List<Item> items) {
               this.items = items;
       }

       public List<Item> getItems() {
               return items;
       }

}

@Entity
public class Item implements Serializable {

       private static final long serialVersionUID = 1L;

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

       @ManyToOne(fetch = FetchType.LAZY)
       private Container container;

       @Column
       private String value;

       public void setKey(Key key) {
               this.key = key;
       }

       public Key getKey() {
               return key;
       }

       public void setContainer(Container container) {
               this.container = container;
       }

       public Container getContainer() {
               return container;
       }

       public void setValue(String value) {
               this.value = value;
       }

       public String getValue() {
               return value;
       }

       @Override
       public String toString() {
               return "Item [key=" + key + ", value=" + value + "]";
       }
}

@Service("TestLocalService")
public class LocalService {

       protected final Log logger = LogFactory.getLog(getClass());


       public Key createContainer() {
               em.getTransaction().begin();
               Container container = new Container();
               em.persist(container);
               em.flush();
               logger.fatal("new container" + container.getKey());
               em.getTransaction().commit();
               return container.getKey();
       }

       public Key generateItems(Key containerKey) {
               em.getTransaction().begin();
               Container container = em.find(Container.class,
containerKey);
               Key key = null;
               for (int i = 0; i < 5; i++) {
                       Item item = new Item();
                       item.setContainer(container);
                       item.setValue(Integer.toString(i));
                       em.persist(item);
                       em.flush();
                       key = item.getKey();
                       logger.fatal("new item" + item);
               }
               em.getTransaction().commit();
               return key;
       }

       public void changeItems(Key containerKey, Key itemKey) {
               em.getTransaction().begin();
               Container container = em.find(Container.class,
containerKey);
               Item changedItem = em.find(Item.class, itemKey);
               changedItem.setValue("xxx"); //
<-----------------------------------
PROBLEM HERE
               em.flush();
               logger.fatal("changed item: " + changedItem);
               for (Item item : container.getItems()) {
                       logger.fatal("list item: " + item); //
<----------------------------------- PROBLEM HERE (Item contains a
stale value of the field <value>)
               }
               em.getTransaction().commit();
       }

       public static void testListSync(LocalService service) {
               // MAIN TEST
               Key containerKey = service.createContainer();
               Key itemKey = service.generateItems(containerKey);
               service.changeItems(containerKey, itemKey);
       }
}


EXECUTION LOG

### CALL createContainer
datastore_v3.BeginTransaction
datastore_v3.Put
datastore_v3.Commit

## LOG createContainer
SEVERE: new containerContainer(684)

### CALL generateItems
datastore_v3.BeginTransaction
datastore_v3.Get
datastore_v3.Put
datastore_v3.Put
datastore_v3.Put
datastore_v3.Put
datastore_v3.Put
datastore_v3.Commit

### LOG generateItems
SEVERE: new itemItem [key=Container(684)/Item(685), value=0]
SEVERE: new itemItem [key=Container(684)/Item(686), value=1]
SEVERE: new itemItem [key=Container(684)/Item(687), value=2]
SEVERE: new itemItem [key=Container(684)/Item(688), value=3]
SEVERE: new itemItem [key=Container(684)/Item(689), value=4]

### CALL changeItems
datastore_v3.BeginTransaction
datastore_v3.Get
datastore_v3.Get
datastore_v3.Put
datastore_v3.RunQuery
datastore_v3.Commit

### LOG changeItems
SEVERE: changed item: Item [key=Container(684)/Item(689),value=xxx] //
<----------------------------------- PROBLEM HERE
SEVERE: list item: Item [key=Container(684)/Item(685), value=0]
SEVERE: list item: Item [key=Container(684)/Item(686), value=1]
SEVERE: list item: Item [key=Container(684)/Item(687), value=2]
SEVERE: list item: Item [key=Container(684)/Item(688), value=3]
SEVERE: list item: Item [key=Container(684)/Item(689), value=4] //
<----------------------------------- PROBLEM HERE

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to