9 Replies Latest reply on Oct 13, 2007 7:41 AM by Christian Zeidler

    Calling a Method on a Home Object

    Christian Zeidler Newbie

      Hello,

      I have created a method on a Home Object of an Entity. I use it to call a web service and gather more Information on that selected entity.

      Now my question is how do I call this method when I display the Information on a JSF-Page...

      I tryed an action in pages.xml for that view, but that doesn't work as I cannot access the Home object before the view is rendered. I can't really understand when the Home object is created in that process at all... (I'm using standard list and entity views generated by seam gen)

      Thanks,
      Christian

        • 1. Re: Calling a Method on a Home Object
          Matt Drees Master

           

          "christian_zeidler" wrote:
          I cannot access the Home object before the view is rendered.


          Why not? I think you should be able to.

          It'd be a good idea to post some code.

          • 2. Re: Calling a Method on a Home Object
            Christian Zeidler Newbie

            Here comes some code... I hope somebody can give me a hint

            Here is the entity:

            @Entity
            public class Foo implements Serializable {
            
             //seam-gen attributes (you should probably edit these)
             private Long id;
             private Integer version;
             private String name;
            
             //add additional entity attributes
            
             //seam-gen attribute getters/setters with annotations (you probably should edit)
            
             @Id @GeneratedValue
             public Long getId() {
             return id;
             }
            
             public void setId(Long id) {
             this.id = id;
             }
            
             @Version
             public Integer getVersion() {
             return version;
             }
            
             private void setVersion(Integer version) {
             this.version = version;
             }
            
             @Length(max=20)
             public String getName() {
             return name;
             }
            
             public void setName(String name) {
             this.name = name;
             }
            }


            Here is the Home object with the printFoo() method I'd like to call when loading the view:
            @Name("fooHome")
            public class FooHome extends EntityHome<Foo>
            {
            
             @RequestParameter
             Long fooId;
            
             public void printFoo(){
             if(this.instance != null){
             System.out.println(this.instance.getName());
             } else{
             System.out.println("no instance");
             }
             }
            
             @Override
             public Object getId()
             {
             if (fooId==null)
             {
             return super.getId();
             }
             else
             {
             return fooId;
             }
             }
            
             @Override @Begin
             public void create() {
             super.create();
             }
            }


            I've got this in pages.xml:
            <page view-id="/foo.xhtml" action="#{fooHome.printFoo}">
             <param name="fooId" value="#{fooHome.id}" />
             </page>


            Now when I call the page I get following logging output:
            17:10:27,848 INFO [STDOUT] no instance
            17:10:27,919 INFO [STDOUT] Hibernate: select foo0_.id as id25_0_, foo0_.name as name25_0_, foo0_.version as version25_0_ from Foo foo0_ where foo0_.id=?


            It tells me that the printFood method is executed before the entity is fetched and therfore the instance is still null. Any idea on how to achieve an other order of execution?

            Thank you,
            Christian

            • 3. Re: Calling a Method on a Home Object
              Christian Bauer Master

              That is currently not doable with the entity framework in Seam and something I recently discussed with Gavin. You can't call getInstance() in either a page action or an @Create method, the identifier binding will happen afterwards (if it's a page parameter).

              Really, there is no obvious solution except not calling getInstance() that early.

              • 4. Re: Calling a Method on a Home Object
                Christian Zeidler Newbie

                OK,

                there is no obvious solution except not calling getInstance() that early


                what would be a good way to call it "later"? SeamRemote and a JavaScript to call the Method from the page once it's redered might be one way, but it sounds like quite some work compared to the page action...

                • 5. Re: Calling a Method on a Home Object
                  Christian Bauer Master

                  I don't know what you want to do, because obviously printing something on the console is not a regular case. You can call getInstance() _after_ the @Create method and _after_ the page action.

                  • 6. Re: Calling a Method on a Home Object
                    Matt Drees Master

                    I'm confused. Doesn't seam gen create edit pages with a wire() page action which calls getInstance()?

                    • 7. Re: Calling a Method on a Home Object
                      Christian Bauer Master

                      Ugh, my bad. I was wrong and confused this with something else (mostly the @Create page param problem). Of course you can call a page action and the entity instance should be available in that page action if its identifier is set as a page parameter.

                      No idea why it doesn't work in that particular case though.

                      • 8. Re: Calling a Method on a Home Object
                        Pete Muir Master

                        Access instance through the getter.

                        • 9. Re: Calling a Method on a Home Object
                          Christian Zeidler Newbie

                          great, thx

                          public void printFoo(){
                           if(this.getInstance() != null){
                           System.out.println(this.getInstance().getName());
                           } else{
                           System.out.println("no instance");
                           }
                          }


                          works fine.