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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:13:33+00:00 2026-05-12T15:13:33+00:00

Can someone please help me to figure out how to create domain classes for

  • 0

Can someone please help me to figure out how to create domain classes for simple one-to-one mapping in grails !

let say we have 2 tables (oracle):

create table table_a(long_common_id_name number(5) primary key using index, 
                     notes varchar2(10 byte), 
                     update_seq number(3)not null );

create table table_b (long_common_id_name number(5) primary key using index, 
                      extra_notes varchar2(200 byte), 
                      update_seq number(3) not null);

alter table table_b add (constraint table_b_fk foreign key (long_common_id_name)
references table_a (long_common_id_name));

I created 2 domain classes:

class TableA {
    static mapping = {
        table 'table_a'
        columns {
            id                  column:'LONG_COMMON_ID_NAME'
            data                column:'NOTES'
            version             column:'UPDATE_SEQ'
        }
    }
    String data
    TableB extraData
}

class TableB {
    static mapping = {
        table 'table_b'
        columns {
            id                  column:'LONG_COMMON_ID_NAME'
            data                column:'EXTRA_NOTES'
            version             column:'UPDATE_SEQ'
        }
    }
    String data
}

This particular definition is not correct. Grails (or Hibernate) builds an incorrect SQL for TableA :

select this_.LONG_COMMON_ID_NAME as LONG1_66_0_, this_.UPDATE_SEQ as UPDATE2_66_0_, this_.NOTES as NOTES66_0_, this_.extra_data_id as extra4_66_0_ from table_a this_

I’ve tried many things: belongsto , hasManey with unique but nothing seems to work.

Thanks in advance.

  • 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-12T15:13:34+00:00Added an answer on May 12, 2026 at 3:13 pm

    Answering my own question, maybe someone will find it useful …

    I’m writing a quick web-ui against our legacy database, so instead of writing lots of SQLs to answer user questions I want automate it. I can not modify data structure and access through UI will be read only.

    Here is solution/workaround that I can live with:

    domain/TableA.groovy

    class TableA {
        static mapping = {
            table 'table_a'
            columns {
                id                  column:'LONG_COMMON_ID_NAME'
                data                column:'NOTES'
                version             column:'UPDATE_SEQ'
            }
        }    
        // will manually handle persistence of TableB
        static transients = [ 'extraData' ]
    
        String data
        TableB extraData
    }
    

    domain/TableB.groovy

    class TableB {
        static mapping = {
            table 'table_b'
            columns {
                id                  column:'LONG_COMMON_ID_NAME'
                data                column:'EXTRA_NOTES'
                version             column:'UPDATE_SEQ'
            }
    
            //actual id is copied from TableA after it is persisted
            id generator:'assigned'
        }
    
        static transients = [ "parent" ]
    
        String data
        TableA parent
    }
    

    the form in “views/tableA/create.gsp” has input fields for TableA and TableB properties, so I will get data for TableA and TableB instances


    In the “controllers/TableAController.groovy” I manually update/delete/load TableB instance

    def list = {    
       ...
       def rv = TableA.list( params)
       rv.each() { it.extraData = TableB.get(String.valueOf(it.id)) }
       ...
    }
    
    def show = {
       ....
       tableAInstance.extraData = TableB.get(tableAInstance.id)
       ...
    }
    def delete = {
       ...
       // delete tableB instance first before tableA
       def tb = TableB.get(tableAInstance.id)
       if (tb) tb.delete()
    
       tableAInstance.delete()
       ...
    }
    
    def save = {
       ....
       // after tableAInstance.save() call
       def tb = new TableB()
       tb.data = params.extraData?.data
       // copy id from tableA instance
       tb.id = tableAInstance.id
       tb.save()
       .....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.