Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8288665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:19:23+00:00 2026-06-08T12:19:23+00:00

I tried to define one to one relation in 2 different way: Grails 2.0.3

  • 0

I tried to define one to one relation in 2 different way:
Grails 2.0.3

Case 1:

class Car {
    String model  
    Engine eng  
    static constraints = {
        eng unique: true
    }
}

class Engine {
    Double capacity
    static belongsTo = [car : Car]
}

Case 2:

class Car {
    String model
    static hasOne = [eng : Engine]
    static constraints = {
        eng unique: true
    }
}


class Engine {
    Double capacity
    static belongsTo = [car : Car]
}

looks similar, and both provide one to one bidirectional mapping. Unfortunately DB has different structure in both cases.

Case 1:
enter image description here

Case 2:
enter image description here

Why once Car and once Engine keeps link to second table.

Where is my problem? When I am looking at the code, from DDD perspective, both cases suggest that Car class is more important and Car aggregates Engine. Unfortunately when I look from DB side on case 2 I would rather said that it is opposite – Engine aggregate Car. Of course I can use first approach, but most of publication I saw about grails, present second way for defining relation. Maybe I misunderstood something and I use hasOne in wrong way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:19:24+00:00Added an answer on June 8, 2026 at 12:19 pm

    The documentation on hasOne states that using this creates a bi-directional one-to-one relationship where the foreign key is on the child.

    The belongsTo means that actions performed on the parent (eg save and update) will be cascaded by hibernate to the child.

    So if you want the foreign key to be on Engine use static hasOne = [engine:Engine] on Car.

    If you want the foreign key to be on Car then use Engine engine on Car .

    In both cases use belongsTo = [car: Car] on Engine

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to split two fields from a binary string: -define(S,<<M\0\0\0522039355099,010100000008,0,010170000000,0,0,0,0,0,0,,,0,0,,‌​0110,00,150,0,0,0\0>>).<<Message_length:4/binary,Msg/binary>> = S. the
I need one property as collection in a model, so i tried associations but
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
Is it possible to get a node by its internalId? I tried: Store Ext.define('myStore',
I tried to run this code: #define ROW_CNT 8; #define COLUMN_CNT 24; #define FIRST_COLUMN
I tried to create base controller and write in it: Ext.define('My.Users.controller.Role', { extend :
Im trying to define an array of arrays as a Constant in one of
I'm trying to have different pictures on every one of my pages built on
I tried to write a (simple, i.e. without eqan? ) one? function like such:
I want to define a macro that randomly chooses one of the given expressions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.