2 Replies Latest reply on Feb 24, 2006 11:27 AM by aidan_b5

    Persistence Manager Strange Behaviour

    aidan_b5

      I cannot get the persistence manager to persist the objects in the following code. No exception is thrown (on persist), the objects are simply not persisted.

      @Stateless
      public class SRSFacadeBean implements SRSFacade{
      
       @PersistenceContext (unitName="srsentity")
      
      
      ......
      
      
      public void createShip(DynaBean db) throws DelegateException
      {
       createEntity();
       Ship ship = new Ship();
       String imo = (String) db.get("imo");
       ship.setImo(imo);
      
       DynaProperty dp[] = db.getDynaClass().getDynaProperties();
       long currenttime = Calendar.getInstance().getTimeInMillis();
      
       ArrayList<Attribute> ala = new ArrayList<Attribute>();
      
       for(DynaProperty dpp: dp)
       {
       String nm = dpp.getName();
      
       if(!nm.equals("imo"))
       {
       Attribute attribute = new Attribute();
       String vl = (String) db.get(nm);
      
       //get attribute name
       AttributeName attributename = null;
       try
       {
       attributename = (AttributeName) manager.createQuery("from AttributeName ana where ana.name = ?1").setParameter(1, nm).getSingleResult();
       }
       catch(NoResultException e)
       {
       log.info("AttributeName not found, creating:" + nm);
       attributename = new AttributeName();
       attributename.setName(nm);
       }
       catch(Exception e)
       {
       log.debug("Multiple AttributeNames (" + nm + ") found, database integrity must be reestablished");
       e.printStackTrace();
       //TODO: Reestablish database integrity!
       }
      
       //set attribute to attributename mapping + vice versa
       attribute.setNameid(attributename);
       attributename.getAttr().add(attribute);
      
       //set values
       attribute.setValue(vl);
       attribute.setStartdate(currenttime);
       attribute.setEnddate(-1);
       attribute.setShip(ship);
       ala.add(attribute);
       manager.persist(attributename);
       manager.persist(attribute);
       }
       }
       ship.setAttributes(ala);
       manager.persist(ship);
      }
      
      
      public void createEntity()
      {
       AnEntityBean aeb = new AnEntityBean();
       aeb.setTotal(111111111);
       manager.persist(aeb);
      
      }
      
      }
      
      


      persistence.xml

      <persistence>
       <persistence-unit name="srsentity">
       <jta-data-source>java:/MySqlDS</jta-data-source>
      
       <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
       </properties>
       </persistence-unit>
      </persistence>



      The bizarre thing is that if my test method, createEntity() is called from outside the Bean, it works fine and AnEntityBean is persisted to the database.
      If I call it from within this class (as I have at the top of createShip(), it doesn't persist AnEntityBean.

      Ship, Attribute and AttributeName are all standard entities mapped to tables, futher code can be posted as required.

      This code falls over because each instance of AttributeName is never persisted, therefore the query returns null.

      Help would be very much appreciated as I feel I'm tired of banging my head against the proverbial brick wall!




        • 1. Re: Persistence Manager Strange Behaviour
          bill.burke

          depending on your id generation, it may not be persisted in the database until commit. Doing the query should cause a flush. Can you do an entityManager.flush() after your persist() calls and see if that helps? If it does help, then it looks like the query execution autoflush is not working.

          • 2. Re: Persistence Manager Strange Behaviour
            aidan_b5

            Found what the problem was; for the record:

            An exception was being thrown because I wasn't cascading Entity.Persist in my ManyToOne relationships.

            Now because the entity manager persists objects are the end of its life (i.e when the method returns from SRSFacadeBean), the transaction was being aborted altogether when the exception was thrown, hence it seemed odd things were happening.

            Thanks anyway.