Package com.avaje.ebean.bean

Examples of com.avaje.ebean.bean.PersistenceContext


            Object id = idBinder.read(ctx);
            if (id == null) {
                return null;
            }

            PersistenceContext persistCtx = ctx.getPersistenceContext();
            Object existing = persistCtx.get(targetType, id);

            if (existing != null) {
                return existing;
            }
            Object ref = targetDescriptor.createReference(ctx.isReadOnly(), id);
           
            EntityBeanIntercept ebi = ((EntityBean) ref)._ebean_getIntercept();
            if (Boolean.TRUE.equals(ctx.isReadOnly())) {
                ebi.setReadOnly(true);
            }
            persistCtx.put(id, ref);
            ctx.register(name, ebi);
            return ref;
        }
View Full Code Here


      return false;
    }
   
    Object ownerBean = bc.getOwnerBean();
    EntityBeanIntercept ebi = ((EntityBean) ownerBean)._ebean_getIntercept();
    PersistenceContext persistenceContext = ebi.getPersistenceContext();

    BeanDescriptor<?> targetDescriptor = many.getTargetDescriptor();

    List<Object> idList = entry.getIdList();
    bc.checkEmptyLazyLoad();
    for (int i = 0; i < idList.size(); i++) {
      Object id = idList.get(i);
      Object refBean = targetDescriptor.createReference(readOnly, id);
      EntityBeanIntercept refEbi = ((EntityBean) refBean)._ebean_getIntercept();

      many.add(bc, (EntityBean) refBean);
      persistenceContext.put(id, refBean);
      refEbi.setPersistenceContext(persistenceContext);
    }
    return true;
  }
View Full Code Here

    }
    return cacheBean; 
  }

  private PersistenceContext getPersistenceContext(SpiTransaction t) {
    PersistenceContext context = null;
    if (t == null) {
      t = desc.getEbeanServer().getCurrentServerTransaction();
    }
    if (t != null) {
      context = t.getPersistenceContext();
View Full Code Here

    ResetBasicData.createOrderCustAndOrder("testPc");

    Transaction t = Ebean.beginTransaction();
    SpiTransaction spiTxn = (SpiTransaction) t;
    PersistenceContext pc = spiTxn.getPersistenceContext();

    System.out.println("pc0:" + pc.toString());

    // no orders or customers in the PC
    Assert.assertEquals(0, pc.size(Order.class));
    Assert.assertEquals(0, pc.size(Customer.class));

    Order order0 = null;
    try {

      EbeanServer server = Ebean.getServer(null);
      List<Order> list = server.find(Order.class).fetch("customer").fetch("details").findList();

      int orderSize = list.size();
      Assert.assertTrue(orderSize > 1);

      // keep a hold of one of them
      order0 = list.get(0);

      System.out.println("pc1:" + pc.toString());
      Assert.assertEquals(orderSize, pc.size(Order.class));

      System.gc();
      Assert.assertEquals(orderSize, pc.size(Order.class));
      Assert.assertTrue(pc.size(Customer.class) > 0);

      list = null;
      // System.gc();

      // transaction still holds PC ...
      // System.out.println("pc2:"+pc);
      // These asserts may not succeed depending on JVM
      // Assert.assertEquals(pc.size(Order.class), 1);
      // Assert.assertEquals(pc.size(Customer.class), 1);

    } finally {
      t.end();
    }

    System.gc();
    System.out.println("pc4:" + pc.toString());

    // we still have the order
    Assert.assertNotNull(order0);
    // its likely the only one in the PC now
    // due to the System.gc(); but can't garuntee it
View Full Code Here

      t.getEvent().addDeleteById(descriptor, id);
    }
    int rows = executeSqlUpdate(deleteById, t);
   
    // Delete from the persistence context so that it can't be fetched again later
    PersistenceContext persistenceContext = ((SpiTransaction)t).getPersistenceContext();
    if (idList != null) {
      for (Object  idValue : idList) {
        persistenceContext.deleted(descriptor.getBeanType(), idValue);
      }
    } else {
      persistenceContext.deleted(descriptor.getBeanType(), id);
    }
    return rows;
  }
View Full Code Here

    int batchSize = getBatchSize(batch.size());

    LoadManyBuffer ctx = loadRequest.getLoadContext();
    BeanPropertyAssocMany<?> many = ctx.getBeanProperty();

    PersistenceContext pc = ctx.getPersistenceContext();

    ArrayList<Object> idList = new ArrayList<Object>(batchSize);

    for (int i = 0; i < batch.size(); i++) {
      BeanCollection<?> bc = batch.get(i);
View Full Code Here

  }

  private void loadManyInternal(EntityBean parentBean, String propertyName, Transaction t, boolean refresh, ObjectGraphNode node, boolean onlyIds) {

    EntityBeanIntercept ebi = ((EntityBean) parentBean)._ebean_getIntercept();
    PersistenceContext pc = ebi.getPersistenceContext();

    BeanDescriptor<?> parentDesc = server.getBeanDescriptor(parentBean.getClass());
    BeanPropertyAssocMany<?> many = (BeanPropertyAssocMany<?>) parentDesc.getBeanProperty(propertyName);

    BeanCollection<?> beanCollection = null;
    ExpressionList<?> filterMany = null;
   
    Object currentValue = many.getValue(parentBean);
    if (currentValue instanceof BeanCollection<?>) {
      beanCollection = (BeanCollection<?>) currentValue;
      filterMany = beanCollection.getFilterMany();
    }

    Object parentId = parentDesc.getId(parentBean);

    if (pc == null) {
      pc = new DefaultPersistenceContext();
      pc.put(parentId, parentBean);
    }

    boolean useManyIdCache = beanCollection != null && parentDesc.isManyPropCaching();
    if (useManyIdCache) {
      Boolean readOnly = null;
View Full Code Here

        // just add the first Id again
        idList.add(firstId);
      }
    }

    PersistenceContext persistenceContext = ctx.getPersistenceContext();

    SpiQuery<?> query = (SpiQuery<?>) server.createQuery(beanType);

    query.setMode(Mode.LAZYLOAD_BEAN);
    query.setPersistenceContext(persistenceContext);
View Full Code Here

  }

  private void refreshBeanInternal(EntityBean bean, SpiQuery.Mode mode, int embeddedOwnerIndex) {

    EntityBeanIntercept ebi = ((EntityBean) bean)._ebean_getIntercept();;
    PersistenceContext pc = ebi.getPersistenceContext();

    BeanDescriptor<?> desc = server.getBeanDescriptor(bean.getClass());
    if (EntityType.EMBEDDED == desc.getEntityType()) {
      // lazy loading on an embedded bean property
      EntityBean embeddedOwner = (EntityBean)ebi.getEmbeddedOwner();
      int ownerIndex = ebi.getEmbeddedOwnerIndex();

      refreshBeanInternal(embeddedOwner, mode, ownerIndex);
    }
   
    Object id = desc.getId(bean);

    if (pc == null) {
      // a reference with no existing persistenceContext
      pc = new DefaultPersistenceContext();
      pc.put(id, bean);
      if (ebi != null) {
        ebi.setPersistenceContext(pc);
      }
    }
View Full Code Here

    BeanDescriptor desc = getBeanDescriptor(type);
    // convert the id type if necessary
    id = desc.convertId(id);

    Object ref = null;
    PersistenceContext ctx = null;

    SpiTransaction t = transactionScopeManager.get();
    if (t != null) {
      // first try the persistence context
      ctx = t.getPersistenceContext();
      ref = ctx.get(type, id);
    }

    if (ref == null) {
      InheritInfo inheritInfo = desc.getInheritInfo();
      if (inheritInfo != null) {
        // we actually need to do a query because
        // we don't know the type without the
        // discriminator value
        BeanProperty idProp = desc.getIdProperty();
        if (idProp == null) {
          throw new PersistenceException("No ID properties for this type? " + desc);         
        }
       
        // just select the id properties and
        // the discriminator column (auto added)
        Query<T> query = createQuery(type);
        query.select(idProp.getName()).setId(id);

        ref = query.findUnique();

      } else {
        // use the default reference options
        ref = desc.createReference(null, id);
      }

      if (ctx != null && (ref instanceof EntityBean)) {
        // Not putting a vanilla reference in the persistence context
        ctx.put(id, ref);
      }
    }
    return (T) ref;
  }
View Full Code Here

TOP

Related Classes of com.avaje.ebean.bean.PersistenceContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.