Java/Hibernate integration with Groovy Domain classes

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

Java/Hibernate integration with Groovy Domain classes

Luis Muniz-2
Hi all,

I am new to grails and I'm very enthusiastic about it. Let me ask you a newbie question:

re:
http://grails.codehaus.org/Hibernate+Integration
http://grails.codehaus.org/Scaffolding

I have read the pages on Hibernate integration and Java integration, if i got it right, what they say is this:

Either use a Groovy domain class, or use a Hibernate/java domain class.

This means that to declare constraints we would have to use the rather verbose way in java, instead of the tidy way provided by builders.

@Transient
public Map getConstraints() {
ConstrainedProperty nameConstraint = new
ConstrainedPersistentProperty(getClass(), "name", String.class );
nameConstraint.setOrder(1);
nameConstraint.setLength (new IntRange(0,30));

List constraints = new ArrayList();
constraints.add(nameConstraint);

return constraints;
}

instead of the beauty here:

@Property constraints = {
name(length:0..30)
}


I have not tried this out yet, but why can't we just have a base class mapped by hibernate, and a groovy domain class inheriting from it?


something like

GenreBase.java being the javabean containing all the mapping annotations...


and then creating a domain class

Genre.groovy extends GenreBase {
 @Property constraints = {

   name(length:0..30)
 }
}


and in the controller using Genre seamlessly.....


Am I daydreaming guys :)?


Reply | Threaded
Open this post in threaded view
|

Re: Java/Hibernate integration with Groovy Domain classes

graemer
On 4/14/06, Luis <[hidden email]> wrote:

> Hi all,
>
> I am new to grails and I'm very enthusiastic about it. Let me ask you a
> newbie question:
>
> re:
> http://grails.codehaus.org/Hibernate+Integration
> http://grails.codehaus.org/Scaffolding
>
> I have read the pages on Hibernate integration and Java integration, if i
> got it right, what they say is this:
>
> Either use a Groovy domain class, or use a Hibernate/java domain class.
>
> This means that to declare constraints we would have to use the rather
> verbose way in java, instead of the tidy way provided by builders.
>
> @Transient
>  public Map getConstraints() {
>  ConstrainedProperty nameConstraint =
new
> ConstrainedPersistentProperty(getClass(), "name",
> String.class );
>  nameConstraint.setOrder(1);
>  nameConstraint.setLength
(new IntRange(0,30));
>
>  List constraints = new ArrayList();
>  constraints.add(nameConstraint);
>
>
return constraints;

>  }
>
> instead of the beauty here:
>
> @Property constraints = {
>  name(length:0..30)
> }
>
>
> I have not tried this out yet, but why can't we just have a base class
> mapped by hibernate, and a groovy domain class inheriting from it?
>
>
> something like
>
> GenreBase.java being the javabean containing all the mapping annotations...
>
>
> and then creating a domain class
>
> Genre.groovy extends GenreBase {
>  @Property constraints = {
>    name(length:0..30)
>  }
> }
>
>
> and in the controller using Genre seamlessly.....
>
>
> Am I daydreaming guys :)?

We´ve already recognised this as a problem and the solution proposed
is to allow the definition of constraints outside of a domain class,
possibly in the controller or in a general configuration class.

I´m not sure if your solution would work as you need some way of
telling hibernate that ´constraints´ is a transient property and as
groovy doesn´t support annotations at the moment this isn´t possible.

The other alternative is of course to write your domain object in
groovy as you would do normally and then map it with hibernate XML

Graeme
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Java/Hibernate integration with Groovy Domain classes

Luis Muniz-2
How about this setup, to circumvent the lack of annotations in groovy?

Country.java contains Hibernate annotations

in Country.java we have

  @Transient
   public List getConstraints() {
    return null;
   }

CountryG.groovy:

class CountryG extends Country{
  @Property constraints = {
        name(length:5..10)
  }
}


in hibernate.cfg.xml we have:
        <mapping class="CountryG" />


now the reality check, it does not work:

   [groovy] DispatcherServlet with name 'grails' received request for [/grails-wm/countryG/list]
   [groovy] Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@e86c03 ]
   [groovy] [SimpleGrailsController] Processing request for uri [/countryG/list]
   [groovy] Processing request for controller [countryG], action [list], and id []
   [groovy] Extra params from uri [{}]
   [groovy] HandlerExceptionResolver returned ModelAndView [ModelAndView: reference to view with name 'error'; model is {exception= org.codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException: groovy.lang.MissingMethodException: No signature of method CountryG.list() is applicable for argument types: (org.codehaus.groovy.grails.web.metaclass.GrailsParameterMap ) values: {["max":10]}}] for exception
   [groovy] Handler execution resulted in exception - forwarding to resolved error view
   [groovy] org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException : No signature of method CountryG.list() is applicable for argument types: (org.codehaus.groovy.grails.web.metaclass.GrailsParameterMap) values: {["max":10]}
   [groovy]     at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke (MetaClassHelper.java:654)
   [groovy]     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:343)
   [groovy]     at groovy.lang.Closure.call(Closure.java:176)
   [groovy]     at groovy.lang.Closure.call (Closure.java:171)
   [groovy]     at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleAction(SimpleGrailsControllerHelper.java:353)
   [groovy]     at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleURI (SimpleGrailsControllerHelper.java:288)
   [groovy]     at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleURI(SimpleGrailsControllerHelper.java:117)
   [groovy]     at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController.handleRequest (SimpleGrailsController.java:78)
   [groovy]     at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
   [groovy]     at org.springframework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet.java:717)
   [groovy]     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:658)
   [groovy]     at org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java:392)
   [groovy]     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:347)
   [groovy]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:596)
   [groovy]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)


I don't know exactly why it doesn't. I can see one problem possibly related with annotation inheritance, but this seems to be that CountryG is not recognized by grails as a domain class, before even getting to this issue.
Reply | Threaded
Open this post in threaded view
|

Re: Java/Hibernate integration with Groovy Domain classes

Luis Muniz-2
In reply to this post by graemer
How about this setup, to circumvent the lack of annotations in groovy?

Country.java contains Hibernate annotations

in Country.java we have

  @Transient
   public List getConstraints() {
    return null;
   }

CountryG.groovy:

class CountryG extends Country{
  @Property constraints = {
        name(length:5..10)
  }
}


in hibernate.cfg.xml we have:
        <mapping class="CountryG" />


now the reality check, it does not work:

   [groovy] DispatcherServlet with name 'grails' received request for [/grails-wm/countryG/list]
   [groovy] Testing handler adapter [org.springframework.web
.servlet.mvc.SimpleControllerHandlerAdapter@e86c03 ]
   [groovy] [SimpleGrailsController] Processing request for uri [/countryG/list]
   [groovy] Processing request for controller [countryG], action [list], and id []
   [groovy] Extra params from uri [{}]
   [groovy] HandlerExceptionResolver returned ModelAndView [ModelAndView: reference to view with name 'error'; model is {exception= org.codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException: groovy.lang.MissingMethodException: No signature of method CountryG.list() is applicable for argument types: (org.codehaus.groovy.grails.web.metaclass.GrailsParameterMap ) values: {["max":10]}}] for exception
   [groovy] Handler execution resulted in exception - forwarding to resolved error view
   [groovy] org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException : No signature of method CountryG.list() is applicable for argument types: (org.codehaus.groovy.grails.web.metaclass.GrailsParameterMap) values: {["max":10]}
   [groovy]     at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke (MetaClassHelper.java:654)
   [groovy]     at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:343)
   [groovy]     at groovy.lang.Closure.call(Closure.java:176)
   [groovy]     at groovy.lang.Closure.call (Closure.java:171)
   [groovy]     at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleAction(SimpleGrailsControllerHelper.java:353)
   [groovy]     at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleURI (SimpleGrailsControllerHelper.java:288)
   [groovy]     at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleURI(SimpleGrailsControllerHelper.java:117)
   [groovy]     at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController.handleRequest (SimpleGrailsController.java:78)
   [groovy]     at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
   [groovy]     at org.springframework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet.java:717)
   [groovy]     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:658)
   [groovy]     at org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java:392)
   [groovy]     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:347)
   [groovy]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:596)
   [groovy]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)


I don't know exactly why it doesn't. I can see one problem possibly related with annotation inheritance, but this seems to be that CountryG is not recognized by grails as a domain class, before even getting to this issue.


On 4/16/06, Graeme Rocher <[hidden email]> wrote:
On 4/14/06, Luis <[hidden email]> wrote:

> Hi all,
>
> I am new to grails and I'm very enthusiastic about it. Let me ask you a
> newbie question:
>
> re:
> http://grails.codehaus.org/Hibernate+Integration
> http://grails.codehaus.org/Scaffolding
>
> I have read the pages on Hibernate integration and Java integration, if i
> got it right, what they say is this:
>
> Either use a Groovy domain class, or use a Hibernate/java domain class.
>
> This means that to declare constraints we would have to use the rather
> verbose way in java, instead of the tidy way provided by builders.
>
> @Transient
>  public Map getConstraints() {
>  ConstrainedProperty nameConstraint =
new
> ConstrainedPersistentProperty(getClass(), "name",
> String.class );
>  nameConstraint.setOrder(1);
>  nameConstraint.setLength
(new IntRange(0,30));
>
>  List constraints = new ArrayList();
>  constraints.add(nameConstraint);
>
>
return constraints;

>  }
>
> instead of the beauty here:
>
> @Property constraints = {
>  name(length:0..30)
> }
>
>
> I have not tried this out yet, but why can't we just have a base class
> mapped by hibernate, and a groovy domain class inheriting from it?
>
>
> something like
>
> GenreBase.java being the javabean containing all the mapping annotations...
>
>
> and then creating a domain class
>
> Genre.groovy extends GenreBase {
>  @Property constraints = {
>    name(length:0..30)
>  }
> }
>
>
> and in the controller using Genre seamlessly.....
>
>
> Am I daydreaming guys :)?

We´ve already recognised this as a problem and the solution proposed
is to allow the definition of constraints outside of a domain class,
possibly in the controller or in a general configuration class.

I´m not sure if your solution would work as you need some way of
telling hibernate that ´constraints´ is a transient property and as
groovy doesn´t support annotations at the moment this isn´t possible.

The other alternative is of course to write your domain object in
groovy as you would do normally and then map it with hibernate XML

Graeme
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Java/Hibernate integration with Groovy Domain classes

graemer
In reply to this post by Luis Muniz-2
I think all of these solutions are work arounds/hacks to something we
should support and will do in the future.

I'm not entirely sure why your example isn't working without trying it
out myself.

Graeme

On 4/17/06, Luis <[hidden email]> wrote:

> How about this setup, to circumvent the lack of annotations in groovy?
>
> Country.java contains Hibernate annotations
>
> in Country.java we have
>
>   @Transient
>    public List getConstraints() {
>     return null;
>    }
>
> CountryG.groovy:
>
> class CountryG extends Country{
>   @Property constraints = {
>         name(length:5..10)
>   }
> }
>
>
> in hibernate.cfg.xml we have:
>         <mapping class="CountryG" />
>
>
> now the reality check, it does not work:
>
>    [groovy] DispatcherServlet with name 'grails' received request for
> [/grails-wm/countryG/list]
>    [groovy] Testing handler adapter
> [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@e86c03
> ]
>    [groovy] [SimpleGrailsController] Processing request for uri
> [/countryG/list]
>    [groovy] Processing request for controller [countryG], action [list], and
> id []
>    [groovy] Extra params from uri [{}]
>    [groovy] HandlerExceptionResolver returned ModelAndView [ModelAndView:
> reference to view with name 'error'; model is {exception=
> org.codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException:
> groovy.lang.MissingMethodException: No signature of method
> CountryG.list() is applicable for argument types:
> (org.codehaus.groovy.grails.web.metaclass.GrailsParameterMap
> ) values: {["max":10]}}] for exception
>    [groovy] Handler execution resulted in exception - forwarding to resolved
> error view
>    [groovy]
> org.codehaus.groovy.runtime.InvokerInvocationException:
> groovy.lang.MissingMethodException : No signature of method
> CountryG.list() is applicable for argument types:
> (org.codehaus.groovy.grails.web.metaclass.GrailsParameterMap)
> values: {["max":10]}
>    [groovy]     at
> org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke
> (MetaClassHelper.java:654)
>    [groovy]     at
> groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:343)
>    [groovy]     at groovy.lang.Closure.call(Closure.java:176)
>    [groovy]     at groovy.lang.Closure.call (Closure.java:171)
>    [groovy]     at
> org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleAction(SimpleGrailsControllerHelper.java:353)
>    [groovy]     at
> org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleURI
> (SimpleGrailsControllerHelper.java:288)
>    [groovy]     at
> org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper.handleURI(SimpleGrailsControllerHelper.java:117)
>    [groovy]     at
> org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController.handleRequest
> (SimpleGrailsController.java:78)
>    [groovy]     at
> org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
>    [groovy]     at
> org.springframework.web.servlet.DispatcherServlet.doDispatch
> (DispatcherServlet.java:717)
>    [groovy]     at
> org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:658)
>    [groovy]     at
> org.springframework.web.servlet.FrameworkServlet.processRequest
> (FrameworkServlet.java:392)
>    [groovy]     at
> org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:347)
>    [groovy]     at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:596)
>    [groovy]     at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
>
>
> I don't know exactly why it doesn't. I can see one problem possibly related
> with annotation inheritance, but this seems to be that CountryG is not
> recognized by grails as a domain class, before even getting to this issue.
>