4 Replies Latest reply on Aug 16, 2005 7:01 PM by jeremyc

    EntityManager.persist saving to database but not updating ob

    jeremyc

      I am using 4.0.3RC2 and have an Entity bean from the EJB3 example (Fund, Investor, etc...). I created a simple app that creates a new Fund and then calls EntityManager.persist(). The data is written to the database but my POJO is not updated with the ID value created.

      OK, further testing. It seems that in the Session bean, the getId() is there and valid but my remote application is not getting it... Here's a few code snipplets:

      Fund.java:

      ...
      
      @Entity
      @Table (name="fund")
      public class Fund implements Serializable {
      
      ...
      
       @Id (generate = GeneratorType.AUTO)
       public int getId() {
       return id;
       }
      
       ...
      


      BankBean.java:
      ...
      
      @Stateless
      public class BankBean implements BankLocal, BankRemote {
       @PersistenceContext (unitName="task")
       EntityManager em;
      
       public void addFund(Fund fund) {
       em.persist(fund);
       System.out.println(fund.getId()); // Correct Output
       }
      
      ...
      


      MyRemoteClient.java:
      ...
      
      public class MyRemoteClient {
       public static void main(String[] args) throws Exception {
       InitialContext ctx = new InitialContext();
       Bank bank = (Bank) ctx.lookup(BankRemote.class.getName());
      
       Fund fund = new Fund();
       fund.setName("Jeremy");
       fund.setGrowthrate(10);
       bank.addFund(fund);
      
       System.out.println(fund.getId()); // 0 (Zero) all the time.
      
      ...
      


      Any ideas?

      Jeremy


        • 1. Re: EntityManager.persist saving to database but not updatin
          bill.burke

          parameters are passed by value when invoking on remote interfaces. Your addFund method must return the id or return the full Fund object back to the client.

          • 2. Re: EntityManager.persist saving to database but not updatin
            jeremyc

            Ok, I changed my code to return the new Fund. But now I am having problems such as:

            Fund fund = new Fund();
            fund.setName("Jeremy");
            fund.setGrowthrate(10);
            fund = bank.addFund(fund);

            System.out.println(fund.getId()); // Works!

            fund.setName("John Doe");
            bank.saveFund(fund); // simply calls em.persist(fund);

            This happens:

            Exception in thread "main" javax.ejb.EJBException: null; CausedByException is:
            detached entity passed to persist: com.cowgar.learn.ejb3.task.ejbs.entity.Fund

            How do I either keep from getting a detached entity or re-attach the entity?

            Thanks,

            Jeremy

            • 3. Re: EntityManager.persist saving to database but not updatin
              bill.burke

              EntityManager.merge()

              Don't be mad, but you are going to have to try out more examples if you want me to help you further. These are simple questions.

              • 4. Re: EntityManager.persist saving to database but not updatin
                jeremyc

                Bill,

                You are right. I will read more. I do wish that there was a tutorial. I have been going through the EJB 3.0 Trail Blazer and have not been having good much luck, but I will persist (he he) and make it through it, I hope.

                I am glad that they are simple problems and not complex issues with how to work around this or that with JBoss :-)

                Thanks,

                Jeremy