Re: How to get dbEntityName from a transient object?

From: Borut Bolčina (borut.bolcin..mail.com)
Date: Wed Jul 23 2008 - 07:36:10 EDT

  • Next message: Aristedes Maniatis: "Re: MissingSessionException"

    Hello,

    this is my shorter solution:
    String tableName =
    context.getEntityResolver().getObjEntity(item.getClass().getSimpleName()).getDbEntityName();

    Thanks for the tip Andrey.

    2008/7/23 Andrey Razumovsky <razumovsky.andrey@gmail.com>:

    > Seems that you're looking just for a name of DB table, to clean it before
    > insert? If so, why cannot you use
    > context.getEntityResolver().getObjEntity(String) method? I don't see why
    > you
    > need that dummyContext in that case
    >
    > 2008/7/23, Borut Bolčina <borut.bolcina@gmail.com>:
    > >
    > > Hello,
    > >
    > > is there a better solution to creating a dummyDataContext just to get
    > > dbEntityName and then unregistering all objects:
    > >
    > > DataContext context = DataContext.createDataContext();
    > > // item is TRANSIENT
    > > CayenneDataObject item = (CayenneDataObject)
    > > allItemsFromOneProvider.iterator().next();
    > > // so create a dummy data context
    > > DataContext dummyContext = DataContext.createDataContext();
    > > dummyContext.registerNewObject(item);
    > > // just to find out table name
    > > logger.debug("item.getObjEntity().getDbEntityName(): " +
    > > item.getObjEntity().getDbEntityName());
    > > String tableName = item.getObjEntity().getDbEntityName();
    > > // now unregister item
    > > dummyContext.unregisterObjects(allItemsFromOneProvider);
    > >
    > > context.performGenericQuery(new NamedQuery("DeleteAll",
    > > Collections.singletonMap("table", tableName)));
    > >
    > > // now do a clean insert
    > > logger.info("Persisting...");
    > > for (Iterator i = allItemsFromOneProvider.iterator();
    > > i.hasNext();) {
    > > item = (CayenneDataObject) i.next();
    > >
    > > context.registerNewObject(item);
    > > }
    > > context.commitChanges();
    > >
    > > Regards,
    > >
    > > Borut
    > >
    >



    This archive was generated by hypermail 2.0.0 : Wed Jul 23 2008 - 07:37:02 EDT