Grails

hasOne mapping is by default eager and cannot be changed to lazy

Details

  • Type: Bug Bug
  • Status: In Progress In Progress
  • Priority: Blocker Blocker
  • Resolution: Unresolved
  • Affects Version/s: 1.2-M2
  • Fix Version/s: 1.2-M3
  • Component/s: Persistence
  • Labels:
    None

Description

I think it should be lazy by default, and be able to set to eager by using custom ORM mappings.

As discussed on twitter you already mentioned the bug to be on line 2061 of http://github.com/grails/grails/blob/7c7ac4efd2f3e1796e3b285edb0ff38aeeccc1d7/grails/src/java/org/codehaus/groovy/grails/orm/hibernate/cfg/GrailsDomainBinder.java

Issue Links

Activity

Hide
Graeme Rocher added a comment -

Seems lazy one-to-one associations are not possible in Hibernate. See https://www.hibernate.org/162.html

It is only possible to make a many-to-one lazy

Show
Graeme Rocher added a comment - Seems lazy one-to-one associations are not possible in Hibernate. See https://www.hibernate.org/162.html It is only possible to make a many-to-one lazy
Hide
Peter Ledbrook added a comment -

I disagree that this should be "Won't fix". As the article linked here and the article linked in the comments of GRAILS-6551 state, the association can be lazy-loaded if the association is mandatory. Since the association is not nullable, it can be lazy-loaded.

So, I still think this is a bug in the mapping, although since it's tied to the constraints it may be difficult or impossible to implement. Would it be possible to add 'constrained=true' to the association mapping if the constraint for the association is 'nullable: false'? Has anyone looked into this?

Show
Peter Ledbrook added a comment - I disagree that this should be "Won't fix". As the article linked here and the article linked in the comments of GRAILS-6551 state, the association can be lazy-loaded if the association is mandatory. Since the association is not nullable, it can be lazy-loaded. So, I still think this is a bug in the mapping, although since it's tied to the constraints it may be difficult or impossible to implement. Would it be possible to add 'constrained=true' to the association mapping if the constraint for the association is 'nullable: false'? Has anyone looked into this?
Hide
Joachim Martin added a comment -

I agree with Peter on this. I think my only option at this point to get lazy loaded one-to-one associations is to map them as one-to-many and refer to the association using property[0]. Really? This doesn't seem right.

In my case I have role-based associations, e.g. User is Mapped one-to-one to Student or Administrator depending on their roles. I end up loading both of those associations if I map them with hasOne even though I don't need fields from either table in the query.

Show
Joachim Martin added a comment - I agree with Peter on this. I think my only option at this point to get lazy loaded one-to-one associations is to map them as one-to-many and refer to the association using property[0]. Really? This doesn't seem right. In my case I have role-based associations, e.g. User is Mapped one-to-one to Student or Administrator depending on their roles. I end up loading both of those associations if I map them with hasOne even though I don't need fields from either table in the query.

People

Vote (1)
Watch (4)

Dates

  • Created:
    Updated: