GROM mapping and EntityPersister

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

GROM mapping and EntityPersister

::SammyRulez::
Hi all

Dealing with a legacy database I'm trying to reuse a Hibernate
EntityPersister (which extends SingleTableEntityPersister ).

I can't figure how to use GROM mapping to add my custom
EntityPersister to Grails domain Objects and I cant find documentation
about it.

Thanks

--
SammyRulez
http://www.sammyrulez.com
http://twitter.com/sammyrulez
http://www.linkedin.com/in/sammyrulez

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GROM mapping and EntityPersister

pledbrook
> Dealing with a legacy database I'm trying to reuse a Hibernate
> EntityPersister (which extends SingleTableEntityPersister ).
>
> I can't figure how to use GROM mapping to add my custom
> EntityPersister to Grails domain Objects and I cant find documentation
> about it.

I'm afraid this doesn't seem possible. Hopefully someone else will be
able to confirm this one way or the other, but I can't see anything in
the code.

Cheers,

Peter

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GROM mapping and EntityPersister

::SammyRulez::
looking at the code of GrailsDomainBinder it would be possible if I
can access the org.hibernate.mapping.PersistentClass of the
DomainClass... any ideas?

2010/5/7 Peter Ledbrook <[hidden email]>:

>> Dealing with a legacy database I'm trying to reuse a Hibernate
>> EntityPersister (which extends SingleTableEntityPersister ).
>>
>> I can't figure how to use GROM mapping to add my custom
>> EntityPersister to Grails domain Objects and I cant find documentation
>> about it.
>
> I'm afraid this doesn't seem possible. Hopefully someone else will be
> able to confirm this one way or the other, but I can't see anything in
> the code.
>
> Cheers,
>
> Peter
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
SammyRulez
http://www.sammyrulez.com
http://twitter.com/sammyrulez
http://www.linkedin.com/in/sammyrulez

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GROM mapping and EntityPersister

::SammyRulez::
Just a followup

the EntityPersister was just an idea to reuse a ready and tested
artifact. Basically I'm interested in overriding  the delete save
methods of GraislDomain Objects.

I'm trying also the plugin approach but something like

def doWithDynamicMethods = { applicationContext ->

    application.domainClasses.each { domainClass ->

      domainClass.metaClass.delete = { ->
        println("HEY!!!!!!!!!!!!!!! Do not delete ever!")
      }
    }

doesn't seem to work.

2010/5/7 ::SammyRulez:: <[hidden email]>:

> looking at the code of GrailsDomainBinder it would be possible if I
> can access the org.hibernate.mapping.PersistentClass of the
> DomainClass... any ideas?
>
> 2010/5/7 Peter Ledbrook <[hidden email]>:
>>> Dealing with a legacy database I'm trying to reuse a Hibernate
>>> EntityPersister (which extends SingleTableEntityPersister ).
>>>
>>> I can't figure how to use GROM mapping to add my custom
>>> EntityPersister to Grails domain Objects and I cant find documentation
>>> about it.
>>
>> I'm afraid this doesn't seem possible. Hopefully someone else will be
>> able to confirm this one way or the other, but I can't see anything in
>> the code.
>>
>> Cheers,
>>
>> Peter
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
>
> --
> SammyRulez
> http://www.sammyrulez.com
> http://twitter.com/sammyrulez
> http://www.linkedin.com/in/sammyrulez
>



--
SammyRulez
http://www.sammyrulez.com
http://twitter.com/sammyrulez
http://www.linkedin.com/in/sammyrulez

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GROM mapping and EntityPersister

pledbrook
> I'm trying also the plugin approach but something like
>
> def doWithDynamicMethods = { applicationContext ->
>
>    application.domainClasses.each { domainClass ->
>
>      domainClass.metaClass.delete = { ->
>        println("HEY!!!!!!!!!!!!!!! Do not delete ever!")
>      }
>    }
>
> doesn't seem to work.

If you increase the logging level of
"org.codehaus.groovy.grails.plugins" to "info", you should see what
order the plugins are loaded. Make sure your plugin is loaded after
"domainClass" and "hibernate". You can control this by adding a

  def loadAfter = [ "hibernate" ]

property to your plugin descriptor.

Hope that helps,

Peter

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GROM mapping and EntityPersister

burtbeckwith
In reply to this post by ::SammyRulez::
You can hook into the Hibernate initialization with a custom Configuration subclass and set the persister there:

package com.yourcompany.yourapp;

import java.util.Collection;

import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration;
import org.hibernate.MappingException;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;

public class MyHibernateConfiguration extends GrailsAnnotationConfiguration {

   private static final long serialVersionUID = 1;

   private boolean _alreadyProcessed;

   @SuppressWarnings("unchecked")
   @Override
   protected void secondPassCompile() throws MappingException {
      super.secondPassCompile();

      if (_alreadyProcessed) {
         return;
      }

      for (PersistentClass pc : (Collection<PersistentClass>)classes.values()) {
         if (pc instanceof RootClass) {
            RootClass root = (RootClass)pc;
            if ("Foo".equals(root.getClassName())) {
               root.setEntityPersisterClass(MyEntityPersister.class);
            }
         }
      }

      _alreadyProcessed = true;
   }
}

To use this, put the class in src/java and register it using the 'configClass' attribute:

dataSource {
   pooled = true
   driverClassName = ...
   username = ...
   password = ...
   configClass = com.yourcompany.yourapp.MyHibernateConfiguration
}

But if you're just trying to block deleting instances, you can use the beforeDelete event hook:

   transient beforeDelete = {
      throw new IllegalAccessException('Delete not allowed')
   }

Burt

> Just a followup
>
> the EntityPersister was just an idea to reuse a ready and tested
> artifact. Basically I'm interested in overriding  the delete save
> methods of GraislDomain Objects.
>
> I'm trying also the plugin approach but something like
>
> def doWithDynamicMethods = { applicationContext ->
>
>     application.domainClasses.each { domainClass ->
>
>       domainClass.metaClass.delete = { ->
>         println("HEY!!!!!!!!!!!!!!! Do not delete ever!")
>       }
>     }
>
> doesn't seem to work.
>
> 2010/5/7 ::SammyRulez:: <[hidden email]>:
> > looking at the code of GrailsDomainBinder it would be possible if I
> > can access the org.hibernate.mapping.PersistentClass of the
> > DomainClass... any ideas?
> >
> > 2010/5/7 Peter Ledbrook <[hidden email]>:
> >>> Dealing with a legacy database I'm trying to reuse a Hibernate
> >>> EntityPersister (which extends SingleTableEntityPersister ).
> >>>
> >>> I can't figure how to use GROM mapping to add my custom
> >>> EntityPersister to Grails domain Objects and I cant find documentation
> >>> about it.
> >>
> >> I'm afraid this doesn't seem possible. Hopefully someone else will be
> >> able to confirm this one way or the other, but I can't see anything in
> >> the code.
> >>
> >> Cheers,
> >>
> >> Peter
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe from this list, please visit:
> >>
> >>    http://xircles.codehaus.org/manage_email
> >>
> >>
> >>
> >
> >
> >
> > --
> > SammyRulez
> > http://www.sammyrulez.com
> > http://twitter.com/sammyrulez
> > http://www.linkedin.com/in/sammyrulez
> >
>
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GROM mapping and EntityPersister

burtbeckwith
Another option is to put the metaclass code in BootStrap. The problem with the plugin approach is that delete() is lazily added to the metaclass when the first dynamic method is called at runtime, so your version will get replaced. But you can force the initialization of the metaclass methods by calling one of them, e.g. get(-1) which is pretty lightweight:

class BootStrap {

   def grailsApplication

   def init = { servletContext ->
      for (dc in grailsApplication.domainClasses) {

         dc.clazz.get(-1) // force lazy metaclass initialization

         dc.clazz.metaClass.delete = { ->
            println("HEY!!!!!!!!!!!!!!! Do not delete ever!")
         }
         dc.clazz.metaClass.delete = { Map m ->
            println("HEY!!!!!!!!!!!!!!! Do not delete ever!")
         }
      }
   }
}


Burt

> You can hook into the Hibernate initialization with a custom Configuration subclass and set the persister there:
>
> package com.yourcompany.yourapp;
>
> import java.util.Collection;
>
> import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration;
> import org.hibernate.MappingException;
> import org.hibernate.mapping.PersistentClass;
> import org.hibernate.mapping.RootClass;
>
> public class MyHibernateConfiguration extends GrailsAnnotationConfiguration {
>
>    private static final long serialVersionUID = 1;
>
>    private boolean _alreadyProcessed;
>
>    @SuppressWarnings("unchecked")
>    @Override
>    protected void secondPassCompile() throws MappingException {
>       super.secondPassCompile();
>
>       if (_alreadyProcessed) {
>          return;
>       }
>
>       for (PersistentClass pc : (Collection<PersistentClass>)classes.values()) {
>          if (pc instanceof RootClass) {
>             RootClass root = (RootClass)pc;
>             if ("Foo".equals(root.getClassName())) {
>                root.setEntityPersisterClass(MyEntityPersister.class);
>             }
>          }
>       }
>
>       _alreadyProcessed = true;
>    }
> }
>
> To use this, put the class in src/java and register it using the 'configClass' attribute:
>
> dataSource {
>    pooled = true
>    driverClassName = ...
>    username = ...
>    password = ...
>    configClass = com.yourcompany.yourapp.MyHibernateConfiguration
> }
>
> But if you're just trying to block deleting instances, you can use the beforeDelete event hook:
>
>    transient beforeDelete = {
>       throw new IllegalAccessException('Delete not allowed')
>    }
>
> Burt
>
> > Just a followup
> >
> > the EntityPersister was just an idea to reuse a ready and tested
> > artifact. Basically I'm interested in overriding  the delete save
> > methods of GraislDomain Objects.
> >
> > I'm trying also the plugin approach but something like
> >
> > def doWithDynamicMethods = { applicationContext ->
> >
> >     application.domainClasses.each { domainClass ->
> >
> >       domainClass.metaClass.delete = { ->
> >         println("HEY!!!!!!!!!!!!!!! Do not delete ever!")
> >       }
> >     }
> >
> > doesn't seem to work.
> >
> > 2010/5/7 ::SammyRulez:: <[hidden email]>:
> > > looking at the code of GrailsDomainBinder it would be possible if I
> > > can access the org.hibernate.mapping.PersistentClass of the
> > > DomainClass... any ideas?
> > >
> > > 2010/5/7 Peter Ledbrook <[hidden email]>:
> > >>> Dealing with a legacy database I'm trying to reuse a Hibernate
> > >>> EntityPersister (which extends SingleTableEntityPersister ).
> > >>>
> > >>> I can't figure how to use GROM mapping to add my custom
> > >>> EntityPersister to Grails domain Objects and I cant find documentation
> > >>> about it.
> > >>
> > >> I'm afraid this doesn't seem possible. Hopefully someone else will be
> > >> able to confirm this one way or the other, but I can't see anything in
> > >> the code.
> > >>
> > >> Cheers,
> > >>
> > >> Peter
> > >>
> > >> ---------------------------------------------------------------------
> > >> To unsubscribe from this list, please visit:
> > >>
> > >>    http://xircles.codehaus.org/manage_email
> > >>
> > >>
> > >>
> > >
> > >
> > >
> > > --
> > > SammyRulez
> > > http://www.sammyrulez.com
> > > http://twitter.com/sammyrulez
> > > http://www.linkedin.com/in/sammyrulez
> > >
> >
> >
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email