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 5987155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:46:27+00:00 2026-05-22T22:46:27+00:00

Must I assign the relationship in each direction? Using the textbook domain classes for

  • 0

Must I assign the relationship in each direction? Using the textbook domain classes for the hasOne relationship, this appears necessary from my testing so far to get the instances to recognize each other:

def face = new Face()
def nose = new Nose()
face.nose = nose
nose.face = face

I don’t know why, though. And it’s unconventionally awkward. I’m expecting there’s a better way.

EDIT:

I need help with the relationship assignment. I do not need information on the mechanics of setting up a hasOne relationship in the domain classes, or a discussion about the wisdom of bi-directional references. I want to know why it takes more than a single statement to set the relationship between a nose instance and a face instance.

My two-statement solution is based on trouble I’m having in a complex application. I am going to try to reproduce my experience in a simple example.

  • 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-05-22T22:46:28+00:00Added an answer on May 22, 2026 at 10:46 pm

    Direct Answer

    The need to set up the relationships, i.e. to assign nose to face and face to node, isn’t really awkward. Hibernate is a RELATIONSHIP mapper, so you need to make the relationships explicit. That said, you can write less code by defining a

    setNose(nose){
       this.nose = nose
       nose.face = this
    }
    

    on Face. Now when you do face.nose = nose, the setter gets invoked for you, and the relationship is setup the way you want.

    General Useful Thoughts

    In general, you do not need to assign the relationship in both directions. It is perfectly valid to have unidirectional or bidirectional relationships.

    However, hasOne definition has very specific implications. The documentation very clearly states the purpose of hasOne is to tell hibernate to put the key that defines the relationship in the child, in this case Nose. If you think carefully about it, you will realize that the relationship should be bidirectional. Here are the thought points:

    1) you define the hasOne on Face (i.e. the parent).
    2) even though you have defined hasOne on the parent, the underlying table that is affected is the child’s table (i.e. the face_id column on Nose)
    3) Since the foreign key is on the child, the child must have a reference to its parent. If it didn’t, you would have a property on the child that is a foreign key but is not related to an object.
    4) Remember, you are using the ORM’s machinery to define the relationship. While you could manually add a face_id field to Nose, and set up the values in code (i.e. manage the relationship yourself and not let the ORM tool do it), the fact that you are using ORM explicitly means the ORM tool will manage the relationship for you.

    EDIT — now reading my answer, I was unconvinced, so I wrote a test. I defined the Face and Nose classes as shown in the hasOne documentation, but I did not define the Face on the Nose. I tried to make it unidirectional. I wrote a test to see what what happen. Here is the test

    class FaceTests extends GroovyTestCase {
    
        public void testStuff(){
            Face face = new Face()
            Nose nose = new Nose()
    
            face.nose = nose;
            face.save(flush:true)
    
            assertNotNull face.id
            assertNotNull nose.id
        }
    
    }
    

    and the result is an exception that contains

    hasOne property [Face.nose] is not bidirectional. Specify the other side of the relationship!
    

    So the framework even makes sure, for you, that when you use hasOne, you have a bidirectional relationship.

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

Sidebar

Related Questions

This problem is very strange and I'm hoping someone can help me. For the
I have a manager class maintaining a list of objects. Each Object has a
I don't think this is currently possible or if it's even a good idea,
I'm trying to find the way to face this situation. Having these tables in
What's the best way to initialize constants or other fields in inherited classes? I
I'm writing a method that forms part of the public interface of a Java
I am currently working on a project which has Employee, Manager entities. At the
I am currently somewhat stuck figuring out an elegant solution to my following problem:
I have a Windows.Forms based .NET desktop application that stores privileged information in a
I am trying to learn assembly language and I need clarification on something. Please

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.