Re: [appengine-java] JPA: Query contains stale data (bug or future)?

2010-04-06 Thread Ikai L (Google)
It looks like you are changing the elements of the List themselves and not
the List. You'll have to mark the object as dirty:

https://groups.google.com/group/google-appengine-java/browse_thread/thread/152cdafdded18611?pli=1&auth=DQAAALsysGgvSS7DADt9mTLjZVIfRcR3Ia0xnDJnZbMuAemwe776lEU-XNeYHi4XHpY5Bifg1xWbsctkBvQx5zML0axYXo2NlqltSM4p3KzBugUeNMAkhXyhYebPm0lQcCpCpuEMiZJ5JGfnPurgwrr9v8ED7wmeYMVS1mNUkJlDtsjLTiBUckaN4zVpHE9A954YDaNF86Ph7tzt99WvOJSlKPpB5IElWR-LPXslY-8v_KJTBvSm2gqRVhib-1IBIh9QxBo

I'm
not completely sure this is what's going on here, as I just did a quick read
through of your code. Can you give this a try?

On Fri, Mar 26, 2010 at 11:47 PM, danblack  wrote:

> Hello.
> I'm using JPA support for managing the App Engine datastore.
> And I've got some problems.
>
> I've got an entity  than contains entities (s).
> Here is my actions.
> I starts transaction then updates entity then flush updates to
> database.
> Than I gets all entities 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 items = new ArrayList();
>
>public void setKey(Key key) {
>this.key = key;
>}
>
>public Key getKey() {
>return key;
>}
>
>public void setItems(List items) {
>this.items = items;
>}
>
>public List 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());
>
>@Autowired
>@Qualifier("jpaTemplate")
>protected JpaTemplate jpaTemplate;
>
>@Transactional(propagation = Propagation.REQUIRED)
>public Key createContainer() {
>Container container = new Container();
>jpaTemplate.persist(container);
>jpaTemplate.flush();
>logger.fatal("new container" + container.getKey());
>return container.getKey();
>}
>
>@Transactional(propagation = Propagation.REQUIRED)
>public Key generateItems(Key containerKey) {
>Container container = jpaTemplate.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));
>jpaTemplate.persist(item);
>jpaTemplate.flush();
>key = item.getKey();
>logger.fatal("new item" + item);
>}
>return key;
>}
>
>@Transactional(propagation = Propagation.REQUIRED)
>public void changeItems(Key containerKey, Key itemKey) {
>Container container = jpaTemplate.find(Container.class,
> containerKey);
>Item changedItem = jpaTemplate.find(Item.class, itemKey);
>changedItem.setValue("xxx"); //
> <---
> P

[appengine-java] JPA: Query contains stale data (bug or future)?

2010-03-28 Thread danblack
Hello.
I'm using JPA support for managing the App Engine datastore.
And I've got some problems.

I've got an entity  than contains entities (s).
Here is my actions.
I starts transaction then updates entity then flush updates to
database.
Than I gets all entities 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 items = new ArrayList();

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

public Key getKey() {
return key;
}

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

public List 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());

@Autowired
@Qualifier("jpaTemplate")
protected JpaTemplate jpaTemplate;

@Transactional(propagation = Propagation.REQUIRED)
public Key createContainer() {
Container container = new Container();
jpaTemplate.persist(container);
jpaTemplate.flush();
logger.fatal("new container" + container.getKey());
return container.getKey();
}

@Transactional(propagation = Propagation.REQUIRED)
public Key generateItems(Key containerKey) {
Container container = jpaTemplate.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));
jpaTemplate.persist(item);
jpaTemplate.flush();
key = item.getKey();
logger.fatal("new item" + item);
}
return key;
}

@Transactional(propagation = Propagation.REQUIRED)
public void changeItems(Key containerKey, Key itemKey) {
Container container = jpaTemplate.find(Container.class,
containerKey);
Item changedItem = jpaTemplate.find(Item.class, itemKey);
changedItem.setValue("xxx"); //
<---
PROBLEM HERE
jpaTemplate.flush();
logger.fatal("changed item: " + changedItem);
for (Item item : container.getItems()) {
logger.fatal("list item: " + item); //
< PROBLEM
HERE
}
}

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)
27.03.2010 6:16:17 ru.englishvocabulary.bl.local.test.LocalService
generateItems

### 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]
27.03.2010 6:16:17 ru.englishvocabulary.bl.local.test.LocalService
genera