6 Replies Latest reply on Oct 2, 2007 12:14 PM by djfjboss

    Semantics of @Name, @DataModelSelection

      In an attempt to learn Seam I am experimenting with a simple CRUD example, partly based on the Yuan/Heute book.

      The integration example shows an edit link as:

      <a href="person.seam?pid=#{fan.id}">Edit</a>


      With the Person class having:
      @Entity
      @Name("person")
      public class Person {
       private long id;
       @Id @GeneratedValue
       public long getId() { return id;}
       public void setId(long id) { this.id = id; }


      and person.xhtml having:
      <h2>Edit #{person.name}</h2>
      <h:form>
      <input type="hidden" name="pid"
       value="#{person.id}"/>


      and the ManagerAction class having:
      @Stateful
      @Name("manager")
      public class ManagerAction implements Manager {
      
       @In (required=false) @Out (required=false)
       private Person person;
      
       // @RequestParameter
       Long pid;
      
       @DataModel
       private List <Person> fans;
      
       @DataModelSelection
       private Person selectedFan;
      
       public void setPid (Long pid) {
       this.pid = pid;
      
       if (pid != null) {
       person = (Person) em.find(Person.class, pid);
       } else {
       person = new Person ();
       }
       }
      
       public Long getPid () {
       return pid;
       }



      Apologies for taking so long to get to the point but the issue I have is that I keep getting property X not found on type Y. I had assumed that a Person instance would be maintained in the session state and injected/outjected as appropriate.

      I had also assumed that the setPid method would be invoked via the edit link and thereby set person to the appropriate entity via the id, but this method isn't even called for me. The fact that the RequestParameter annotation is commented out is a little disconcerting.

      To make this work, up to a point, I have had to add @Scope(SESSION) to the entity and session beans but I still find that I am faced with a newly constructed Person instance rather than the one I thought I was operating on and that had been outjected into the shared context.

      In desperation, I've also tried using the @DataModelSelection but without success - property not found again.

      I suspect I'm missing something fundamental and have read about bijection and studied the example which look straightforward enough but something is missing and I can't figure out what it is!



        • 1. Re: Semantics of @Name, @DataModelSelection

           

          "djfjboss" wrote:
          I had also assumed that the setPid method would be invoked via the edit link and thereby set person to the appropriate entity via the id, but this method isn't even called for me. The fact that the RequestParameter annotation is commented out is a little disconcerting.


          The pid attribute will not be set unless you uncomment the @RequestParameter annotation (which will directly inject the value) or add an entry in your pages.xml which will call your set method such as,
          <pages>
           <page view-id="/person.xhtml">
           <param name="pid" value="#{manager.id}"/>
           </page>
           ...
          </pages>


          I also do not see where fan is being outjected to the page so I don't know where your id would be coming from. Are you providing a list of persons and then the user can edit a person from the list? If you are looking to achieve a RESTful URL here (which is the only reason I could think of for doing it this way), an easier way to accomplish this would be...

          @Stateful
          @Name("manager")
          public class ManagerAction implements Manager {
          
           @RequestParameter
           Long pid;
          
           @In (required=false) @Out (required=false)
           private Person person;
          
           @DataModel
           private List <Person> fans;
          
           @Factory(value="person")
           public void loadPerson(Long pid) {
           if (pid != null) {
           person = (Person) em.find(Person.class, pid);
           } else {
           person = new Person ();
           }
           }
           ...
          


          Also note that you will not be in the same conversation when the Person is loaded because you are using a general link <a href ...>. Any conversation state will not be propagated. Hope that helps.

          • 2. Re: Semantics of @Name, @DataModelSelection

            Sorry, the factory method in the above snippet should read,

            ...
            @Factory(value="person")
             public void loadPerson() {
             if (pid != null) {
             person = (Person) em.find(Person.class, pid);
             } else {
             person = new Person ();
             }
             }
            ...


            No value should be passed into the loadPerson method.

            • 3. Re: Semantics of @Name, @DataModelSelection

              Many thanks once again for your help.

              I'm finding progress with Seam frustratingly slow. It sounds like a very powerful technology that should be a great productivity aid but, although the examples look straightforward, when I come to try them there are hidden subtleties and inconsistencies that continue to trip me up (perhaps they're not so straightforward!).

              In addition there are oddities such as the commented out @RequestParameter annotation that's not even explained in the book. I'm reading Yuan/Heute and Nusairat in parallel, neither is the best text book I've ever encountered - the RoR books put them both to shame.

              Do you have any suggestions for better sources of understanding? A clear explanation of the underlying concepts and the semantics of the annotations would hopefully go a long way to dispelling some of my confusion and frustration.

              • 4. Re: Semantics of @Name, @DataModelSelection

                There is a new release of the Yuan/Heute book coming out (the examples in the current release are a little out of date for the latest release of Seam), but likely not until early next year some time. I will be posting an article soon that may help understanding of the semantics of conversations (it covers conversations and nested conversations) along with an example, but the best documentation out there that I've seen is the Seam reference manual.

                I read the Nusairat book when I was getting started but past that I've kept the Seam source in my IDE at all times. I've heard that Java Persistence with Hibernate (Bauer, King) provides a good introduction to Seam, but I have not yet read this text. The forum here is great and I would recommend posting JIRA issues as you have difficulties with the documentation (the Seam team is very good about resolving these) as it will help everybody out. Sorry I can't be of more help.

                • 5. Re: Semantics of @Name, @DataModelSelection

                  By the way, to demonstrate responsiveness to documentation issues based on your conversationList difficulties the other day:

                  http://jira.jboss.com/jira/browse/JBSEAM-1999

                  Resolved in a matter of days. Thanks Seam team!

                  • 6. Re: Semantics of @Name, @DataModelSelection

                    I've read the Seam chapter in the 800 page Hibernate tome, and all the rest of it bar one chapter. The book is generally quite dense but is very readable in the most part. The chapter on Seam was what got me interested in Seam. Given his writing quality, a book on Seam by Gavin King would probably be very useful, but that's probably asking too much given all the other demands on his time!

                    Many thanks once again for your support and tolerance of my stumblings and suggestions for further reading. It certainly is encouraging to see that the JBoss team have already acted on your documentation observations.