GORM Matching Manifest :: Long or Short

The main problem is enough, but its roots go deep within the framework (and there is little specific information on this topic), so I post it here to save others from pain (and make sure that I am right or wrong in my thinking).

What is the problem?

Grails automatically enters a Long id field into your domains ( see Beckwith comment ). When using the legacy DB Grails database, Longs to bigints uses an inefficient storage type by default that anyone who deals with large millions of record tables would avoid.

Having discovered this a few months ago, I started working on getting the “right” column type for my domain identifiers. Without Java background, blindly, I thought, “Long-bad”, “Integer-good” and “Configure hibernation dialogs” for “Integer” types for what I would do manually in mysql

registerColumnType (Types.INTEGER, 'mediumint unsigned')

and then defined "Integer id" in all my domains (not necessarily according to Burt's comment in the link above). Everything worked smoothly, perfectly, for other things.

Speed ​​up to Grails 2.0 (since I couldn't resist all the pluses ;-)) and Spock. For life, I could not understand why, despite the 2.0 new GORMs in memory and support for dynamic crawlers, Domain.findByFoo (fooVal) always returns null (yes, I @Mock (Domain) and fills the test data). In fact, both within the test itself and for the purpose of @TestFor, the only GORM methods that worked were save () and get (); everything else is returned null.

I hacked the quick test application, domain + controller + spoc spec and found the source of the problem: if you use a property type other than Long for other identifiers (including links to FK), your @Mock domain will be useless.

So, can I fix or not say that I need to use Long ids to take full advantage of the framework? @Mock + @TestFor + Spock - an incredible combo! The guide was appreciated before I dropped the refactoring path to the end ...

+4
source share
1 answer

I cannot imagine a realistic scenario where the difference between Integer and Long could make a noticeable difference in performance (which, apparently, was the original motivation for making this change).

If using Long works, but Integer causes problems, then use Long and move on to more important tasks. I would only worry about this if you can prove that using Integer makes any significant difference.

Update

You're right, I completely skipped the point about the type of database that Grails automatically uses for Long. As you seem to already know, you can control the type of database when you close the display of your domain class, for example.

class Person { Long id static mapping = { // See here for alternatives to integer, and how they map to DB types // http://docs.jboss.org/hibernate/stable/core/manual/en-US/html/mapping.html#mapping-types-basictypes id type:'integer' } } 

You also raised in the comments

using Long at the code level, where you need to specify def foo (Long id) {...} and params.id.toLong (), unlike Integer

You can simply define the action as

 def myAction = {Person p -> } 

or

 def myAction = { Person p = new Person(params) } 

And Grails will take care to convert the id request parameter to Person.id , regardless of whether it is Long, Integer, BigDecimal, etc.

+5
source

All Articles