force hibernate to refresh a collection

1,305 views
Skip to first unread message

sas

unread,
Aug 28, 2011, 3:37:14 PM8/28/11
to play-framework
I have the following entities

a Feed can have many Categories

So in the page I update feeds, I have several checkboxes, one for each
category

the page is something like this

<input type="checkbox" name="feed.categories[1].id" id="category6"
value="1">
<input type="checkbox" name="feed.categories[2].id" id="category6"
value="2">
<input type="checkbox" name="feed.categories[3].id" id="category6"
value="3">

If I only check the second check box, Play autobind the Feed with a
ListCategory like this ArrayList( [null, Category(id=xx), null] )

that is, for each unchecked check play takes a null object

the data is saved ok to the db, null are just ignored

Even after saving the entity, the collection keeps the null elements.
and If get the entity again with findById it's the same, the
collection doesn't get updated

I want to get Feed.categories, with only the category that was checked

how can I tell hibernate to refresh that collection agagin from db???


sas

unread,
Aug 28, 2011, 4:22:00 PM8/28/11
to play-framework
Well I've found the answer here

http://groups.google.com/group/play-framework/browse_thread/thread/8756a87252127d0

just had to issue

public static void update(Feed feed) {

....

feed.save

// and after the save

Session jpaSession = (Session) JPA.em().getDelegate();
jpaSession.refresh(feed)

// and know in feed.categories I've got the correct values...

Ronald Haring

unread,
Aug 29, 2011, 8:53:58 AM8/29/11
to play-fr...@googlegroups.com
Shouldnt you just redirect to the page to fetch the data again from the db? That way the playframework sees a new request coming in and will start another session and thus find your just saved categories

Regards
Ronald

sas

unread,
Aug 29, 2011, 5:53:05 PM8/29/11
to play-framework
yes, I also redirect

the problem is that, to increase performance, I saved in a
categorizations_count variable the quantity of categories...

I could've counted the non null elements in Event.categories, but I
thought it would be cleaner to refresh the entity

any better solution you can think of?

saludos

sas
Reply all
Reply to author
Forward
0 new messages