6 Replies Latest reply on Oct 16, 2006 8:04 PM by Owen Jacobson

    EJb 3.0 primary key loose ?

    stephan opitz Newbie

      i make a customer object persistent

      // create a new customer
      customer = new Customers();

      // fill with data
      customer.setEmail(email);
      customer.setPassword(password);

      // make persistent
      customersFacade.save(customer);

      after persists i know the new id is in db 11, but if i log its 0
      why it won*t set automattically
      log.info("customer ID: " + customer.getCustomersId());

      entity has this:

      @Entity
      @Table(name = "customers")
      public class Customers implements Serializable {

      // ------------------------------------------------------ Manifest Constants

      private static final long serialVersionUID = 5L;

      // ------------------------------------------------------- Public Properties

      private long customersId;

      private String firstName;

      private String lastName;

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "customers_id")
      public long getCustomersId() {
      return customersId;
      }

      ...

      should work or how it works?

        • 1. Re: EJb 3.0 primary key loose ?
          Emmanuel Bernard Master

          you need to show the code for
          // make persistent
          customersFacade.save(customer);

          local or remote bean?

          • 2. Re: EJb 3.0 primary key loose ?
            stephan opitz Newbie

            the call for my ejbs from my application is local

            but at this point i'm in the ejbs and have access only access to injected ejbs

            private @EJB
            OrdersFacade ordersFacade;

            all are stateless, except sessionFacade which i set in my application to get the correct on and have access to correct data


            /**
            * The SessionFacade with access to the specific ejb
            */
            private SessionFacade sessionFacade = null;

            // ------------------------------------------------------- Public Properties

            /**
            * @param sessionFacade
            * The sessionFacade to set
            */
            public void setSessionFacade(SessionFacade sessionFacade) {
            this.sessionFacade = sessionFacade;
            }

            • 3. Re: EJb 3.0 primary key loose ?
              stephan opitz Newbie

              and interface defined hier

              import java.util.Date;

              import de.Test.entity.Orders;

              public interface OrdersFacade {

              /**
              * Makes a Orders object persistent
              *
              * @param order
              * Orders object
              */
              public void save(Orders order);

              and statelessbean here
              with mark of which interface is local and remote - both same :-/
              but local isn't need is'nt it - filled i mean?

              import javax.ejb.Local;
              import javax.ejb.Remote;
              import javax.ejb.Stateless;
              import javax.persistence.EntityManager;
              ...
              import de.Test.entity.Orders;
              import de.Test.facade.OrdersFacade;

              @Local(OrdersFacade.class)
              @Remote(OrdersFacade.class)
              @Stateless
              public class OrdersBean implements OrdersFacade {

              @PersistenceContext(unitName = "TestPU")
              private EntityManager em;

              // ------------------------------------------------------ Manifest Constants

              public static final String Remote = OrdersBean.class.getSimpleName()
              + "/remote";

              public static final String Local = OrdersBean.class.getSimpleName()
              + "/local";

              • 5. Re: EJb 3.0 primary key loose ?
                stephan opitz Newbie

                /**
                * Makes a Orders object persistent
                *
                * @param orders
                * Orders object
                */
                public void save(Orders orders) {
                em.persist(orders);
                }
                needknowledge of getting id into object

                additionally could i say

                java persistence api is:
                entity beans + entity manager + hsqldb(which was introduced by hibernate)

                • 6. Re: EJb 3.0 primary key loose ?
                  Owen Jacobson Newbie

                  Try this:

                  in the CustomerFacade:

                  public Orders save(Orders orders) {
                   em.persist(orders);
                   return orders;
                  }


                  Then your persist code looks like

                  // make persistent
                  customer = customersFacade.save(customer);