cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sridhar devatha <devatha.srid...@gmail.com>
Subject Re: Modification and deletion of data objects
Date Sat, 25 Jul 2009 08:11:00 GMT
mike and andrus,

please check the code below. It worked perfectly for all the crud
operations. I am giving here all the CRUD except Creation(which any one can
find do easily).

   public void modifyEmployee(EmployeeBO aEmployeeBO) {
       EmployeeDO aEmployeeDO = this.getEmployeeDOByID(aEmployeeBO,true);
        EmployeeAdapter.populateEmployeeDO(aEmployeeBO,aEmployeeDO);
        this.commitChanges();
    }

    public void deleteEmployee(EmployeeBO aEmployeeBO) {
       EmployeeDO aEmployeeDO = this.getEmployeeDOByID(aEmployeeBO,true);
        EmployeeAdapter.populateEmployeeDO(aEmployeeBO,aEmployeeDO);
        this.getDataContext().deleteObject(aEmployeeDO);
        this.commitChanges();
    }

    private EmployeeDO getEmployeeDOByID(EmployeeBO anEmployeeBO, boolean
fromLocalCache) {
        Expression employeeIdMtEQ =
ExpressionFactory.matchExp(EmployeeDO.EMPLOYEE_ID_PROPERTY,
anEmployeeBO.getEmployeeId());
        SelectQuery ocfsq = new
SelectQuery(EmployeeDO.class,employeeIdMtEQ);

        if(fromLocalCache){
            ocfsq.setCachePolicy(QueryMetadata.LOCAL_CACHE);
        }

        List ocfList = this.getDataContext().performQuery(ocfsq);

       EmployeeDO  aEmployeeDO = (EmployeeDO)ocfList.get(0);

        return aEmployeeDO;
    }

If there is any problem/issue/blunder/dumb things, please do inform me.
Thank you andrus for informing /reminding me about  caches. Moreover i have
one last question. Assuming i disabled container managed transaction. Can I
use implicit cayenne transaction and explicit cayenne programmatic
transaction in one application?

Thank  you once again for your help andrus. Made my work a lot easier. I did
whole crud dao thing in less than 2days as a beginner including
checkout/checkins(version manager is also new , visual svn server with
tortoise svn client. thanks alot for these people who provided these
things), testing with test ng (testing of complete crud done for only one
data object though).

regards,
sridhar.

On Thu, Jul 23, 2009 at 10:58 PM, Mike Kienenberger <mkienenb@gmail.com>wrote:

> Devatha,
>
> Cayenne is not a perfect fit for what you are asking to do.   You are
> going to have to either write some supporting code to make it do what
> you want or work within the design of Cayenne.   You can certainly
> subclass DataContext and add your own extensions to it.
>
> I don't know of anyone using Cayenne with Transfer Objects to business
> objects.   I tried writing a couple of projects like that, but my
> experience was that it's not a useful way to design applications since
> my business objects were practically identical to my Cayenne objects.
>  I found that making the Cayenne objects conform to a business
> interface worked much better for my projects, which avoided the
> overhead of transferring state between objects.
>
> While you can use the DataContext as the manager for tracking data
> objects, it wasn't designed for that purpose, so you might have to
> live with some inefficiencies, like having to search through the
> entire list of modified or new objects rather than the list for a
> specific class.   Again, though, you can certainly make your own
> modifications to DataContext so that registering an object also
> indexes it by class and persistence state and then query that index if
> that's something you care about.
>
> On Thu, Jul 23, 2009 at 2:14 AM, sridhar
> devatha<devatha.sridhar@gmail.com> wrote:
> > one more thing is that method descriptions in cayenne api are not
> complete
> > in the general exception flow.
> > When I saw datacontext class api, i found that there is no way i can get
> the
> > list of new/modified objects of particular data object class.
> > I am getting a dumb idea that whenever I convert data object to business
> > object, i will invalidate that data object. As I don't exactly use the
> same
> > object during modification. There may be no side effect for me. Next
> time, I
> > use objectForQuery to fetch the object again, apply the changes and
> commit.
> > it is making additional select query unnecessarily as there is no way of
> > getting registered data object from data context without using object id.
> > On Thu, Jul 23, 2009 at 10:42 AM, sridhar devatha <
> devatha.sridhar@gmail.com
> >> wrote:
> >
> >> mike, transfer object design pattern might be a good example why I don't
> >> expose data objects in view layer. see
> >> http://java.sun.com/blueprints/patterns/TransferObject.html
> >> Then if i have to use separate objects (that is transfer objects) in
> view
> >> layer. How do you want me to do the conversion to and from business
> object?
> >> I have to include object id/ entity name in the transfer / view object
> to
> >> build business object. I don't want to do that.
> >>   On Thu, Jul 23, 2009 at 10:10 AM, sridhar devatha <
> >> devatha.sridhar@gmail.com> wrote:
> >>
> >>> I can not use any data context, entity resolver, entity name and object
> >>> id, as I did not keep the data object. Keeping the reference to data
> object
> >>> is redundant, as data context is already maintaining them. Cayenne did
> not
> >>> provide a way for getting the registered data object using unique
> keys(not
> >>> primary keys! 99% of tables will have column(s) to uniquely identify a
> table
> >>> row.) I recommend to provide checkboxes to indicate the unique keys in
> >>> modeler. that way, a uniqueObject (it is somewhat similar to primary
> key
> >>> class in EJB. But, it does not use primary keys, only uses unique keys)
> can
> >>> be created using the unique columns related properties by the cayenne
> >>> modeler. This   unique object can be used to retrieve the data object
> >>> registered with the data context(say lookupDataObject() or
> getDataObject()
> >>> in DataContext) by comparing one or more unique key related property
> >>> values.  If there is no such registered data object, these methods
> should
> >>> make a data base call internally and get the data object of the matched
> >>> database table row. If neither data object nor the table row exists,
> this
> >>> method should return null or throw some exception.
> >>> By the way, I want to use DataObjectUtils.objectForQuery() as I did not
> >>> retain neither object id nor entity name of the data object(I don't
> want to
> >>> retain them in business object / view object.). I think this
> objectForQuery
> >>> will hit the data base instead of first looking in the data context. Is
> not
> >>> it? It is time to provide this functionality so that it can be really
> easy
> >>> to do basic crud operations using cayenne. When cayenne provides the
> methods
> >>> to get the list of new and modified objects. How come it does not
> provide
> >>> the basic method of getting the registered object? It is absolutely
> >>> surprising and shocking to me!!!
> >>>
> >>> About the mike's reply, caching or creating interfaces forces me
> >>> substantially to focus on technical things rather than on developement
> of
> >>> project functionality. I absolutely think retrieving the existing
> registered
> >>> data object should be very fundamental method of cayenne. Is not it?
> >>> Moroever, I would rather say that data context should take the burden
> of
> >>> removing data objects and overwriting the existing data object with the
> new
> >>> object that is getting created, if you don't provide methods for
> getting the
> >>> existing registered objects in the data context.There is a great reason
> not
> >>> to use the data object in business layer / view layer. Even if you use
> data
> >>> object in business layer, what should I send to view layer.How can I
> convert
> >>> business object to view object and vice versa without using context,
> entity
> >>> resolver,entity name and object id. I am involved in several projects
> where
> >>> data objects are not at all exposed to view or business layer. One
> purpose
> >>> of using business object is to insulate business layer from the
> internal
> >>> changes of data objects/ data access layer . same is the case with view
> >>> object.
> >>>
> >>> If you need any further info related to above things, ask me. I will be
> >>> available during weekends only.
> >>> Moreover, I am getting doubts about the declarative transactions. Do
> you
> >>> provide them?If yes , tell me how?
> >>> To sum up,
> >>> Please answer about not providing methods for getting the existing
> >>> registered objects from data context?
> >>> Please answer how objectForQuery() works?That is, whether it looks in
> the
> >>> data context before hitting the database.
> >>> Please answer about declarative transactions using a good example.
> >>>
> >>> thanks and regards,
> >>> sridhar
> >>>   On Thu, Jul 23, 2009 at 12:48 AM, Mike Kienenberger <
> mkienenb@gmail.com
> >>> > wrote:
> >>>
> >>>> No, but here's the two templates that are most relevent.   This is
> >>>> probably against Cayenne 1.2 (2.0 with different package names).
> >>>>
> >>>> If they're helpful, great!  If not, sorry, it's as-is at the moment
--
> >>>> it's really not that difficult to come up with your own.  I might have
> >>>> more up-to-date versions, but that's what's in front of me at the
> >>>> moment.
> >>>>
> >>>> On Wed, Jul 22, 2009 at 2:58 PM, Emanuele Maiarelli<evvdrak@yahoo.it>
> >>>> wrote:
> >>>> > Did u made em public accessible? (sourceforge or kinda)
> >>>> >
> >>>> >
> >>>> >
> >>>> >
> >>>> > ________________________________
> >>>> > Da: Mike Kienenberger <mkienenb@gmail.com>
> >>>> > A: user@cayenne.apache.org
> >>>> > Inviato: Mercoledì 22 luglio 2009, 20:52:25
> >>>> > Oggetto: Re: Modification and deletion of data objects
> >>>> >
> >>>> > I wrote templates to generate interfaces as well as concrete classes
> >>>> > for entities.
> >>>> > It's been awhile since I looked at them, so I don't know if they're
> >>>> > suitable for public consumption.
> >>>> >
> >>>> > On Wed, Jul 22, 2009 at 2:36 PM, Emanuele Maiarelli<
> evvdrak@yahoo.it>
> >>>> wrote:
> >>>> >> Hi agree with Mike, unless you have previous designed objects,
i'd
> >>>> take DataObjects
> >>>> >> as business objects.
> >>>> >>
> >>>> >> If u get previous designed objects u could work on the package
> >>>> 'entities' implementing
> >>>> >> methods do adapt DataObjects to your needs. IDEs (at least
> NetBeans)
> >>>> got refactoring services
> >>>> >> that can help u extracting interfaces from your classes, quickly.
> >>>> >>
> >>>> >>
> >>>> >>
> >>>> >>
> >>>> >>
> >>>> >>
> >>>> >> ________________________________
> >>>> >> Da: Mike Kienenberger <mkienenb@gmail.com>
> >>>> >> A: user@cayenne.apache.org
> >>>> >> Inviato: Mercoledì 22 luglio 2009, 20:21:32
> >>>> >> Oggetto: Re: Modification and deletion of data objects
> >>>> >>
> >>>> >> I'm not aware of anyone else using separate objects for the
> business
> >>>> >> level.  I use the DataObjects as the business level objects.
 Yes,
> if
> >>>> >> you decide to manually recreate data objects from business
objects,
> >>>> >> there will be some overhead.  If I had to go that route, I'd
create
> a
> >>>> >> central management class and have it cache what objects I already
> know
> >>>> >> exist in the data context the first time I reference them rather
> than
> >>>> >> searching through the data context lists each time.
> >>>> >>
> >>>> >> One possiblity you could consider is to generate interfaces
for
> your
> >>>> >> DataObjects and then pass the interface rather than the DataObject
> >>>> >> concrete class.  This is what I tend to do in my projects.
  That
> way,
> >>>> >> you can hide all of the Cayenne-specific methods from your
other
> code
> >>>> >> if you want.   Other than some dependencies on Cayenne classes
and
> a
> >>>> >> couple of other methods, there's not a lot of difference between
a
> >>>> >> Cayenne data object and a "cayenne-free" business representation
of
> >>>> >> the same data.
> >>>> >>
> >>>> >> On Wed, Jul 22, 2009 at 2:15 PM, sridhar
> >>>> >> devatha<devatha.sridhar@gmail.com> wrote:
> >>>> >>> andrus, it is not clear(thank for reply though), i have
to try
> over
> >>>> weekend.
> >>>> >>> moreover, i can not use context, getEntityName in business
object.
> I
> >>>> wrote
> >>>> >>> business object to data object conversion in data object
class it
> >>>> self. so I
> >>>> >>> can use cayenne java api.  I have to do it in the data
object
> class
> >>>> which is
> >>>> >>> sub class _data object, as it will have reference of business
> object.
> >>>> But,
> >>>> >>> any way I am just surprised to see that there is no modification
> >>>> example
> >>>> >>> code. Nobody uses cayenne data object from view layer to
> >>>> business/service
> >>>> >>> layer to data access layer in web or enterprise
> applications(because
> >>>> it is
> >>>> >>> cayenne data object with several other sensitive methods
> available.
> >>>> even if
> >>>> >>> the methods are not there. I don't want to couple all the
layers.
> ).
> >>>> I don't
> >>>> >>> want to expose that sensitive object. more concrete examples
will
> >>>> make it
> >>>> >>> easier for new developers like me.
> >>>> >>> I think what Mike Kienenberger(thanks mike , that is what
I am
> also
> >>>> thinking
> >>>> >>> at least. by the way what do you say about andrus reply)
is
> telling
> >>>> may be
> >>>> >>> correct. then i have to go thru all the new and modified
> registered
> >>>> objects
> >>>> >>> in the data context to see if there is any thing existing.
clearly
> >>>> this kind
> >>>> >>> of searching will make it difficult for every data object
updation
> >>>> and
> >>>> >>> deletion. what do you say.
> >>>> >>>
> >>>> >>> On Wed, Jul 22, 2009 at 3:45 PM, Andrus Adamchik <
> >>>> andrus@objectstyle.org>wrote:
> >>>> >>>
> >>>> >>>> In your business object you will need to know 3 things
to map it
> to
> >>>> a
> >>>> >>>> Cayenne object: entity name, id, state (new or already
> persistent).
> >>>> Then you
> >>>> >>>> can do something like this (pseudo code of course):
> >>>> >>>>
> >>>> >>>> X businessObject = ...;
> >>>> >>>> DataObject cayenneObject;
> >>>> >>>> Class cayenneObjectClass = context.getEntityResolver().
> >>>> >>>>
> >>>> getClassDescriptor(businessObject.getEntityName()).getObjectClass();
> >>>> >>>> if(bObject.isNew()) {
> >>>> >>>>  cayenneObject = context.newObject(cayenneObjectClass);
> >>>> >>>> }
> >>>> >>>> else {
> >>>> >>>>  cayenneObject = DataObjectUtils.objectForPK(cayenneObjectClass,
> >>>> >>>> businessObject.getId());
> >>>> >>>> }
> >>>> >>>>
> >>>> >>>> // merge fields...
> >>>> >>>>
> >>>> >>>> Hope I answered the right question.
> >>>> >>>>
> >>>> >>>> Andrus
> >>>> >>>>
> >>>> >>>>
> >>>> >>>> On Jul 22, 2009, at 7:47 AM, sridhar devatha wrote:
> >>>> >>>>
> >>>> >>>>  Hi,
> >>>> >>>>>
> >>>> >>>>> I have view layer, business layer and data access
layer. Each
> have
> >>>> >>>>> separate
> >>>> >>>>> class for carrying data. But, in data access layer,
the object /
> >>>> class
> >>>> >>>>> used
> >>>> >>>>> for carrying data is respective data objects. I
don't send these
> >>>> objects
> >>>> >>>>> beyond my data access layer. Rather I create business
objects
> >>>> populated
> >>>> >>>>> with
> >>>> >>>>> data object data. When the business functions call
data access
> >>>> layer for
> >>>> >>>>> any
> >>>> >>>>> of the CRUD operations, they pass these business
objects. I
> convert
> >>>> these
> >>>> >>>>> business objects to data objects(So, I will create
data object
> >>>> every time
> >>>> >>>>> I
> >>>> >>>>> need to convert business object to data object.
So, what happens
> >>>> when the
> >>>> >>>>> two data objects(one dataobject is created while
retrieving
> data.
> >>>> We send
> >>>> >>>>> the data as business object to business layer.
one data object
> is
> >>>> created
> >>>> >>>>> when the user returns the modified  data in the
form of business
> >>>> object)
> >>>> >>>>> of
> >>>> >>>>> the same class exist with the same data? How can
I modify and
> >>>> delete the
> >>>> >>>>> respective rows of these data objects? Please answer
any body. I
> >>>> did not
> >>>> >>>>> find any example that does the modification.
> >>>> >>>>>
> >>>> >>>>>
> >>>> >>>>> Yours Sincerely,
> >>>> >>>>> Devatha Sridhar
> >>>> >>>>>
> >>>> >>>>
> >>>> >>>>
> >>>> >>>
> >>>> >>>
> >>>> >>> --
> >>>> >>>
> >>>> >>> Yours Sincerely,
> >>>> >>> Devatha Sridhar
> >>>> >>>
> >>>> >>
> >>>> >>
> >>>> >>
> >>>> >>
> >>>> >
> >>>> >
> >>>> >
> >>>> >
> >>>>
> >>>
> >>>
> >>>
> >>> --
> >>>
> >>> Yours Sincerely,
> >>> Devatha Sridhar
> >>>
> >>
> >>
> >>
> >> --
> >>
> >> Yours Sincerely,
> >> Devatha Sridhar
> >>
> >
> >
> >
> > --
> >
> > Yours Sincerely,
> > Devatha Sridhar
> >
>



-- 

Yours Sincerely,
Devatha Sridhar

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message