Quantcast

Reusing Java hibernate entities in a Grails 2 app - redefine column name ?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Reusing Java hibernate entities in a Grails 2 app - redefine column name ?

366388
Im reusing a java hibernate model in a grails app. I've solved most issues but have problem with grails not figuring out that a column is called 'userId'. The generated sql shows 'user_id'  (an underscore)

heres the mapping in the java entity

@OneToOne
@JoinTable(
name = "ql_user2hotel",
joinColumns = {@JoinColumn(name = "hotelId")},
inverseJoinColumns = {@JoinColumn(name = "userId")}
)
private User user;


I really dont want to touch the java entities (as im copying them from one module to another in my during my buiid lifecycle), so my question is this:
 Is it possible to define a new file to override the mapping via groovy meta magic a bit like you can constraints?

or is there another way around this ?

hope you can help

thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Reusing Java hibernate entities in a Grails 2 app - redefine column name ?

Jonathan Rosenberg
I've run into this kind of problem a number of times & never found a
great answer.

The "real" answer is to tell Hibernate to use a different naming
strategy in the SessionFactory (so it won't map names for the
session).  I tried this in many different ways using Grails 1.3.7 &
eventually gave up. I just couldn't make it work.  I wasted several
days trying to make this work.

Are you using Grails 2.X?  maybe it can be done now in a Datasource config file?

Is this SessionFactory being created by Grails?

--
Jonathan Rosenberg
Founder & Executive Director
Tabby's Place, a Cat Sanctuary
http://www.tabbysplace.org/


On Fri, May 4, 2012 at 4:30 PM, 366388 <[hidden email]> wrote:

> Im reusing a java hibernate model in a grails app. I've solved most issues
> but have problem with grails not figuring out that a column is called
> 'userId'. The generated sql shows 'user_id'  (an underscore)
>
> heres the mapping in the java entity
>
> @OneToOne
> @JoinTable(
> name = "ql_user2hotel",
> joinColumns = {@JoinColumn(name = "hotelId")},
> inverseJoinColumns = {@JoinColumn(name = "userId")}
> )
> private User user;
>
>
> I really dont want to touch the java entities (as im copying them from one
> module to another in my during my buiid lifecycle), so my question is this:
>  Is it possible to define a new file to override the mapping via groovy meta
> magic a bit like you can constraints?
>
> or is there another way around this ?
>
> hope you can help
>
> thanks
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Reusing-Java-hibernate-entities-in-a-Grails-2-app-redefine-column-name-tp4609685.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> 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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Reusing Java hibernate entities in a Grails 2 app - redefine column name ?

366388
yes im using Grails 2

everything else in the grails app is the same apart from the java entities and the mapping file I added..


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        '-//Hibernate/Hibernate Configuration DTD 3.0//EN'
        'http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd'>

<hibernate-configuration>

        <session-factory>
        <mapping package='com.ql.entity' />
        <mapping class="com.ql.entity.Country"/>
        <mapping class="com.ql.entity.Hotel"/>
        <mapping class="com.ql.entity.User"/>
        <mapping class="com.ql.entity.UserOld"/>
        <mapping class="com.ql.entity.UserNew"/>
        <mapping class="com.ql.entity.Neighborhood"/>
        <mapping class="com.ql.entity.Hotel"/>
        <mapping class="com.ql.entity.City"/>
        <mapping class="com.ql.entity.HotelInfo"/>
        <mapping class="com.ql.entity.HotelAttribute"/>
       
        </session-factory>

</hibernate-configuration>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Reusing Java hibernate entities in a Grails 2 app - redefine column name ?

Jonathan Rosenberg-2

Unfortunately, you can only specify the naming strategy programmatically, not via a cfg file.  That stinks.

Are you using a Groovy config for the datasource for these mappings?

On May 4, 2012 4:46 PM, "366388" <[hidden email]> wrote:
yes im using Grails 2

everything else in the grails app is the same apart from the java entities
and the mapping file I added..


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
       '-//Hibernate/Hibernate Configuration DTD 3.0//EN'
       'http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd'>

<hibernate-configuration>

       <session-factory>
       <mapping package='com.ql.entity' />
       <mapping class="com.ql.entity.Country"/>
       <mapping class="com.ql.entity.Hotel"/>
       <mapping class="com.ql.entity.User"/>
       <mapping class="com.ql.entity.UserOld"/>
       <mapping class="com.ql.entity.UserNew"/>
       <mapping class="com.ql.entity.Neighborhood"/>
       <mapping class="com.ql.entity.Hotel"/>
       <mapping class="com.ql.entity.City"/>
       <mapping class="com.ql.entity.HotelInfo"/>
       <mapping class="com.ql.entity.HotelAttribute"/>

       </session-factory>

</hibernate-configuration>


--
View this message in context: http://grails.1312388.n4.nabble.com/Reusing-Java-hibernate-entities-in-a-Grails-2-app-redefine-column-name-tp4609685p4609755.html
Sent from the Grails - user mailing list archive at Nabble.com.

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

   http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Reusing Java hibernate entities in a Grails 2 app - redefine column name ?

366388
yes, does that make a difference ..

environments {
    development {
        dataSource {
            url = "jdbc:mysql://localhost/FS"
            driverClassName = "com.mysql.jdbc.Driver"
            username = "root"
            password = "root"

        }
    }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Reusing Java hibernate entities in a Grails 2 app - redefine column name ?

Jonathan Rosenberg-2

Try this

http://stackoverflow.com/questions/3709374/how-can-i-change-column-name-convention-in-grails

It didn't work for neo in 1.3.7, but maybe it works now.  Let us know.

On May 4, 2012 5:45 PM, "366388" <[hidden email]> wrote:
yes, does that make a difference ..

environments {
   development {
       dataSource {
           url = "jdbc:mysql://localhost/FS"
           driverClassName = "com.mysql.jdbc.Driver"
           username = "root"
           password = "root"

       }
   }

--
View this message in context: http://grails.1312388.n4.nabble.com/Reusing-Java-hibernate-entities-in-a-Grails-2-app-redefine-column-name-tp4609685p4609901.html
Sent from the Grails - user mailing list archive at Nabble.com.

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

   http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Reusing Java hibernate entities in a Grails 2 app - redefine column name ?

366388
brilliant ! it worked

thanks a million
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Reusing Java hibernate entities in a Grails 2 app - redefine column name ?

Jonathan Rosenberg-2

Glad it worked.  If I can get around to upgrading to 2.x, I can finally replace my crufty workarounds.

On May 4, 2012 7:05 PM, "366388" <[hidden email]> wrote:
brilliant ! it worked

thanks a million

--
View this message in context: http://grails.1312388.n4.nabble.com/Reusing-Java-hibernate-entities-in-a-Grails-2-app-redefine-column-name-tp4609685p4610062.html
Sent from the Grails - user mailing list archive at Nabble.com.

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

   http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Reusing Java hibernate entities in a Grails 2 app - redefine column name ?

366388
by the way have you had any trouble with constraints for your Java entites in your grails app ?

I've done what the manual says and added e.g. a HotelConstraints.groovy file inside the java directory with same package structure but it just seems to get ignored:

i tried all different approaches:

package xxxx.entity

constraints = {
    //kayakId(validator: {
    //   val, obj ->
    //        log.info "test"
    //      ((val !=null)&&(val > 0))
    //})
    //kayakId(nullable:false, min:1)
    //kayakId validator: {
      //  return it.length != 0
    //}
    kayakId validator: {   val, obj ->
            return val.length != 0
    }

}

by the way do I have access to the logger in here ?

Also, I notice that the field I am trying to validate is a Long object. When I check its contents in the controller just before the save it is just empty i.e. it is not null and has no number assigned which seems strange.  - thats also why im resorting to custom validation above

I also tried adding this to config.groovy but  no joy

grails.validateable.classes = [xxxx.entity.Hotel]


Loading...