-
1. Re: list in application scope
damianharvey.damianharvey.gmail.com Mar 7, 2008 11:01 AM (in response to umajeric)Why don't you get rid of the table and make it a static list? That way you won't need the database access at all and won't be at risk of Lazy Init Exceptions or detached entities.
Cheers,
Damian.
-
2. Re: list in application scope
umajeric Mar 7, 2008 11:22 AM (in response to umajeric)But still what if I wan't to have this in database. Is there any solution?
-
3. Re: list in application scope
nickarls Mar 7, 2008 11:23 AM (in response to umajeric)Use a query hint to use cache. Still better than having it in application scope, I think
-
4. Re: list in application scope
baz Mar 7, 2008 11:24 AM (in response to umajeric)Your task is very easy to achieve.
Make a seam component in ApplicationScope.@Name(value ="astroList") @AutoCreate @Scope(value = ScopeType.APPLICATION) public class AstroListManager { @SuppressWarnings("unchecked") @Unwrap public List<astroelem> astrolistFactory(){ return //yourlist; } }
Under the name astroList you have access to your List. The factory method will be only called if astroList is not set in any Context.
The documentation for seam will explain this in detail.
(Factory and Manager Components)
If you have further question please do not hesitate to ask.Ciao,
Carsten -
5. Re: list in application scope
umajeric Mar 7, 2008 11:26 AM (in response to umajeric)I forgot to say that I have also some table that doesn't change verry often but it still changes. What about in this case?
-
6. Re: list in application scope
nickarls Mar 7, 2008 11:30 AM (in response to umajeric)The second level cache could be useful here, too. You could have the cache expired after 10 minutes or so (if it's ok that the change would come to effect at the next expire and not immediately).
-
7. Re: list in application scope
baz Mar 7, 2008 11:31 AM (in response to umajeric)Look at my solution but set the scope to SESSION. That is what i have done in this case
-
8. Re: list in application scope
umajeric Mar 7, 2008 11:35 AM (in response to umajeric)Thanks for your help.
Uros
-
9. Re: list in application scope
damianharvey.damianharvey.gmail.com Mar 7, 2008 11:41 AM (in response to umajeric)And for extra value use @Observer to refresh parts of the list when an @Event is raised that changes some of the data. This way you don't have to refresh the whole list on every change, it only needs to refresh part of it.
Cheers,
Damian.
-
10. Re: list in application scope
christian.bauer Mar 7, 2008 11:47 AM (in response to umajeric)
Carsten Höhne wrote on Mar 07, 2008 11:24 AM:
The factory method will be only called if astroList is not set in any Context.This is not correct, read the documentation of @Unwrap.
-
11. Re: list in application scope
baz Mar 7, 2008 3:23 PM (in response to umajeric)As you see i have mixed the explanation of @Factory and @Unwrap
The quoted sentence is correct if it refers to a method annotated with @Factory.With unwrap use the example in the docu (hens), which is quiet more correct than my short piece of code:-( And it solves this problem
-
12. Re: list in application scope
umajeric Mar 7, 2008 6:32 PM (in response to umajeric)I did this now, but it doesn't work as it should. The problem is:
when first user accesses the page it works as it should, but when the seccond user accesses the page its sign isn't selected (list is positioned on 'Select your sign'). I implemented method on Signspublic boolean equals(Object obj) { System.out.println("obj: "+obj+", this: "+this); if (obj instanceof Sign) { return (this.getId() == ((Sign)obj).getId()); } return false; }
I tried to output what does it compares (System.out).
This is what it outputs on first access:obj: null, this: (id: 1, name: Ram) obj: (id: 1, name: Ram), this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram) obj: null, this: (id: 1, name: Ram)
And this outputs when I access the page seccond time:
obj: null, this: (id: 2, name: Bull) obj: (id: 1, name: Ram), this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull) obj: null, this: (id: 2, name: Bull)
and the signs Factory class lokks like this:
@Name("signs") @Scope(ScopeType.APPLICATION) public class Signs { @In private EntityManager entityManager; List<Sign> signs; @Unwrap public List<Sign> getSigns() { if (signs == null) { signs = entityManager.createQuery("FROM Sign").getResultList(); } return signs; } }
and the Facelets code:
<h:selectOneMenu value="#{currentUser.sign}"> <s:selectItems value="#{signs}" var="sign" label="#{messages[sign.name]}" noSelectionLabel="#{messages.sign_selectionLabel}"/> <s:convertEntity/> </h:selectOneMenu>
it looks like a bug to me.
Is it?Uros
-
13. Re: list in application scope
trouby Mar 7, 2008 7:08 PM (in response to umajeric)is your sign.id is primitive? make sure it is,
otherwise, if your sign.id is an object Long/Integer than you have to use equals()
Asaf.
-
14. Re: list in application scope
umajeric Mar 9, 2008 1:10 AM (in response to umajeric)sign.id type is int.
I still don't know if this is a bug or I made a mistake?Uros