12 Replies Latest reply on Aug 13, 2007 3:59 PM by pmuir

    Using stateless beans generated by seam in standalone Java a

    jbossding

      I am pretty new to seam framework but have 3 years experience with previous versions of EJB. Our project is using seam to generate codes for a web application to access backend database. It works pretty good. However, I have the need to access the same database in standalone Java applications outside of Jboss virtual machine. When I looked at the seam-generated code, I am lost and need help.

      First how to look up session bean in my Java application? I can not find the ejb-jar.xml so I do not know what is the jndi name used for the generated beans. Second, I do not know how to call those generated beans. For example, I have a table called Host in a test database, the table is simple with id, hostname, and connect_time. Use seam setup, seam new-project, seam generate-entities, and seam explode, seam framework did almost everything for me. It generates two java files in the action folder. Here is the code: in HostList.java

      @Name("hostList")
      public class HostList extends EntityQuery {
      
       private static final String[] RESTRICTIONS = {"lower(host.hostname) like concat(lower(#{hostList.host.hostname}),'%')",};
      
       private Host host = new Host();
      
       @Override
       public String getEjbql() {
       return "select host from Host host";
       }
      
       @Override
       public Integer getMaxResults() {
       return 25;
       }
      
       public Host getHost() {
       return host;
       }
      
       @Override
       public List<String> getRestrictions() {
       return Arrays.asList(RESTRICTIONS);
       }
      
      }


      in HostHome.java

      @Name("hostHome")
      public class HostHome extends EntityHome<Host> {
      
       public void setHostId(Integer id) {
       setId(id);
       }
      
       public Integer getHostId() {
       return (Integer) getId();
       }
      
       @Override
       protected Host createInstance() {
       Host host = new Host();
       return host;
       }
      
       public void wire() {
       }
      
       public boolean isWired() {
       return true;
       }
      
       public Host getDefinedInstance() {
       return isIdDefined() ? getInstance() : null;
       }
      
      }


      By looking at the code, I am lost at how I should call the bean to make select, update, delete, and insert operations.

      Would anyone give me some help or point me to some documents or tutorials? Thanks a lot.



        • 2. Re: Using stateless beans generated by seam in standalone Ja
          jbossding

          enzhao,

          Thanks for the reply. However, I am looking for solution to use seam generated session/entity beans in standalone Java client, not JSF. My standalone Java client has no user interface at all, let alone JSF. Once the ejb code generated by seam-gen, I would prefer no code modification at all. Actually, I am starting to wonder if the seam-generated code is standard ejb3 because I did not find @stateless in HostHome.java or HostList.java.

          Thanks and waiting for more help.

          • 4. Re: Using stateless beans generated by seam in standalone Ja
            ntsankov

             

            "JbossDing" wrote:
            ... My standalone Java client has no user interface at all, let alone JSF. Once the ejb code generated by seam-gen, I would prefer no code modification at all. Actually...


            ROFL
            Nice one :) Good laugh

            • 5. Re: Using stateless beans generated by seam in standalone Ja
              jbossding

              The link above does not help me but I still appreciate it. I need idea if it is possible to use seam-generated code in plain java application.

              Based on my simple understanding of seam-gen, not seam, if I have a database with tables, I can use seam-gen to generate a web application in 5 minutes. That is terrific. However, I have another plain java application also needs to access and manipulate the database. I do not want to code another data-access layer. I would like to use the code generated by seam-gen. This standalone Java application will run out of JBoss and I wonder if my java code can use the seam-gen generated data-access code.

              Why it concerns me is the code generated by seam-gen seems not ejb3 (without @stateless etc.) and I do not know how could standalone java application look it up using JNDI (there is no ejb-jar.xml to define jndi binding).

              Could someone tell if it is doable, and if so, give me some direction. Many thanks.



              • 6. Re: Using stateless beans generated by seam in standalone Ja
                trickyvail

                The entity beans generated by seam-gen are EJB3 beans. I don't see any reason why you should not be able to use them directly in a stand alone java application (outside of JBoss). You will have to create an application managed persistence context to do so.

                • 7. Re: Using stateless beans generated by seam in standalone Ja
                  trickyvail

                  Here is an example of an "Application-Managed Entity Manager" from the book "Pro EJB 3"

                  public class EmployeeClient {
                   public static void main(String[] args) {
                   EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService");
                   EntityManager em = emf.createEntityManager();
                  
                   Collection emps = em.createQuery("SELECT e FROM Employee e")
                   .getResultList();
                   for(Iterator i = emps.iterator(); i.hasNext();) {
                   Employee e = (Employee) i.next();
                   System.out.println(e.getId() + ", " + e.getName());
                   }
                  
                   em.close();
                   emf.close();
                  }
                  * from page 117

                  • 8. Re: Using stateless beans generated by seam in standalone Ja
                    jbossding

                    Thanks for the reply and code example. But I thought the code generated by seam-gen is not ejb (please refer to my above first message). I am very shallow on seam so far. When I follow the seam tutorial of the Registration example, everything is making sense, like you will code the session bean with annotation @stateless and entity bean with @entity. What's more, there is a ejb-jar.xml for deployment descriptor and each bean has remote methods that client can call, for example method register().

                    However, if you look closely at the generated code as I posted above, for table host, seam-gen generated 2 java files in src/Action folder, which I think should be where session beans are. They HostHome.java and HostList.java. By looking at those code closely, they do not have annotation @stateless or @entity. What is more, I do not know what remote methods will execute CRUD operations. For example, in HostHome.java, it has setHostId, getHostId, isWired, wire, getDefinedInstance. If I need to update the records, what remote method to call?

                    I am not very familiar with JPA. I thought the code you put in the last message is for client to use JPA to query database. That is terrific. However, because seam-gen generated data-access layer codes like session bean and entity beans, I would like to be lazy and use the generated codes in client directly.

                    After a little reading of seam, it occurs to me that seam-gen generated data-access code in src/Action folder is not suitable for direct call from a standalone java application. Please correct me if I am wrong. I really need some jboss seam guru to tell me if there is a way for standalone java client (replacing jsf) to reuse seam-gen generated data-access codes. seam-gen is great, but it's just too mysterious for me to us in a not so usual way.

                    Thanks for your attention nd help

                    • 9. Re: Using stateless beans generated by seam in standalone Ja
                      matt.drees

                      Seam lets you use Ejb3 session beans or plain java beans as components. (See the documentation linked to by enzhao). Many of the examples use session beans. Seam-gen creates apps that use java beans. So no, you couldn't remotely use them.

                      However, I don't think it'd take too much work to turn them into Ejb3 session beans. You'd have to create a business interface for them and tag them with @Stateful. I think that's it, though don't quote me on that.

                      • 10. Re: Using stateless beans generated by seam in standalone Ja
                        pmuir

                        Seam-gen doesn't generate EJB3 SFSBs/SLSBs but JavaBean Seam components. As Matt says, you can probably convert them into EJB3s easily, and then, as long as you do a JNDI lookup, you should be fine. This isn't the right forum to ask about accessing EJB3s from a java app (try the EJB3 forum as a start).

                        • 11. Re: Using stateless beans generated by seam in standalone Ja
                          jbossding

                          Hi Pete and Matt, Thanks for the reply. I just wonder is there a way for seam-gen to generate EJB session beans instead of seam javabean components. In seam-gen setup, I specify it is ear project, but it still generates javabean. Thanks

                          • 12. Re: Using stateless beans generated by seam in standalone Ja
                            pmuir

                            Not currently. I don't think there is a JIRA feature request for this open either.