5 Replies Latest reply on Jun 26, 2005 11:03 AM by leonell

    Re: Entity EJB3 Example with Hibernate as persistance Layer

    leonell

      Hello,
      you cannot obtain entity bean (XText) directly.
      You must create session bean and remote interface.
      Then in session bean you can use EntityManager to obtain (persist, remove,..) entity beans.
      From client you ask remote interface of this session bean and call its method (and obtain detached entity bean or if you want, DTOs ).

      See tutorial of JBOSS EJB3 distribution.

      Leonell

        • 1. Re: Re: Entity EJB3 Example with Hibernate as persistance La
          sanjaya5

          RTFM, my friend :}

          • 2. Re: Entity EJB3 Example with Hibernate as persistance Layer
            sanjaya5

            Hey, I'm using San's accout for posting, becouse I get only exception if I want to register.

            OK, I created a Remote Interface and a Stateful Session Bean. If I want to access to the Remote Interface I get only $proxy[64|63|74|...] so I get a TypeCastException.
            client

            ...
            public String getResult() {
             System.out.println("Bitch");
             try {
             InitialContext ctx = new InitialContext();
             System.out.println(XText.class.getName());
             XText xtext = (XText) ctx.lookup(XText.class.getName()); // Type Cast Exception
             /*xtext.sayHello();
             System.out.println("Hello World");*/
             return XText.class.getName();
             }catch(NamingException e){
             e.printStackTrace();
             return "Hello";
             }
             }...
            RemoteInterface
            package de.anwend.ejb;
            
            import javax.ejb.Remote;
            import javax.ejb.Remove;
            
            @Remote
            public interface XText
            {
             void setText(String text);
             String getText();
             @Remove void remove();
             void sayHello();
            }
            Stateful Bean
            package de.anwend.ejb;
            import javax.ejb.Remove;
            import javax.ejb.Stateful;
            import javax.persistence.EntityManager;
            
            
            @Stateful
            public class XTextBean implements XText
            {
             private EntityManager manager;
             private XTextEntity xtext;
            
             public void setText(String text) {
             if(xtext == null) xtext = new XTextEntity();
             xtext.setText(text);
             }
            
             public String getText() {
             return xtext.getText();
             }
            
             public void sayHello() {
             System.out.println("Hello fucking world");
             }
            
             @Remove
             public void remove()
             {
             manager.persist(xtext);
             }
            }
            Entity Bean
            package de.anwend.ejb;
            
            import java.io.Serializable;
            
            import javax.persistence.GeneratorType;
            import javax.persistence.Id;
            import javax.persistence.Table;
            
            import org.hibernate.annotations.Entity;
            
            @Entity
            @Table(name = "xyz")
            public class XTextEntity implements Serializable{
             private Long id;
             private String text;
            
             @Id(generate = GeneratorType.AUTO)
             public Long getId() {
             return id;
             }
            
             public void setId(Long id) {
             this.id = id;
             }
            
             public String getText() {
             return text;
             }
            
             public void setText(String text) {
             this.text = text;
             }
            }
            
            Exception
            javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.faces.el.EvaluationException: Error getting property 'result' from bean of type de.anwend.bean.Ausgabe: java.lang.ClassCastException: $Proxy63


            Regards
            Michael Ernst

            PS: Yes, he knows, that I'm using his account

            • 3. Re: Entity EJB3 Example with Hibernate as persistance Layer
              leonell

              Michael,
              in Topic "Problem with removing item from collection in detached object" is example of EJB3 application (EB, SB, interface and client) for 1:N mapping.
              Use it as base for your application.

              In your code are at least these errors:
              - you dont inject Entity manager (@Inject, @PersistenceContext in Beta)
              - you don't work with EB (see find, persist, merge method of EntityManager).

              You also use org.hibernate.annotations.Entity instead javax.persistence.Entity (I dont know if here is some difference).

              If problem still occurs then undeploy your ejb3 project and deploy it again and check server.log for ERROR.

              Leonell


              • 4. Re: Entity EJB3 Example with Hibernate as persistance Layer
                sanjaya5

                OK, thanks!

                I don't want to use, your Bean becouse I think it's a little bit to complicated to be an easy example of EJB3.
                I deleted my XText.ejb3 from the server.
                Now I have copied the entity Bean example of jboss (jboss-EJB-3.0_Preview_5\docs\tutorial\entity). I also copied the cliend's code to the managed Bean. If I deploy the whole example it looks very well becouse tables are createt and so on. But if is start the Client I get the same error message

                 InitialContext ctx = new InitialContext();
                 ShoppingCart cart = (ShoppingCart) ctx.lookup(ShoppingCart.class.getName()); // Exception
                
                2005-06-26 12:50:38,543 DEBUG [com.sun.faces.el.ValueBindingImpl] getValue(ref=ausgabe.result)
                2005-06-26 12:50:38,663 DEBUG [com.sun.faces.application.ApplicationImpl] Created bean ausgabe successfully
                2005-06-26 12:50:38,663 DEBUG [com.sun.faces.application.ApplicationImpl] Storing ausgabe in scope request
                2005-06-26 12:50:38,663 DEBUG [com.sun.faces.el.VariableResolverImpl] resolveVariable: Resolved variable:de.anwend.bean.Ausgabe@1125a40
                2005-06-26 12:50:38,863 DEBUG [javax.faces.el.PropertyResolver] Error getting property 'result' from bean of type de.anwend.bean.Ausgabe: java.lang.ClassCastException: $Proxy70
                java.lang.ClassCastException: $Proxy70
                 at de.anwend.bean.Ausgabe.getResult(Ausgabe.java:18)
                 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


                What can I do to get a running example! I don't think that ist very difficult. Why I get a $proxy and not the RemoteInterface?

                Regards
                Michael Ernst

                • 5. Re: Entity EJB3 Example with Hibernate as persistance Layer
                  leonell

                  Michael,
                  I don't know if this is solution of your problem but check if you have all needed JAR files in your client classpath (and in correct version ).

                  I use the following packages (Beta1 - I mean but for Preview 5 was the same packages):

                  commons-logging-api.jar
                  commons-logging.jar
                  jboss-ejb3.jar
                  jboss-ejb3x.jar
                  jboss-aop-jdk50.jar
                  jbossall-client.jar
                  jboss-aspect-library-jdk50.jar
                  hibernate3.jar
                  hibernate-metadata.jar
                  ejb3-persistence.jar
                  hibernate-entitymanager.jar
                  jaxen-1.1-beta-4.jar
                  jboss-remoting.jar

                  You can found all these libraries in your JBOSS distribution.

                  Not all are needed * !!!

                  * But it works for me and I don't have reason to search their subset for now...

                  Hope it helps.

                  Leonell