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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:31:27+00:00 2026-06-02T16:31:27+00:00

I am developing a web based application in Grails. I have come across a

  • 0

I am developing a web based application in Grails. I have come across a situation where I would like to try and suppress GORM from creating a foreign key constraint on a field in a table.

I have a domain class which is part of a class hierarchy. The domain class essentially acts as a link to a target domain. The target domain can be of different types and each of the subclasses of this link domain is designed to provide the link for each specific type of linked item. These linked items have certain behaviour in common i.e. implement the same interface but otherwise are different to the point that they are stored in different tables.

Within this link domain table there is one column which represents the id of the item being linked to. All the linked items have the same integer based id. The problem is that GORM tries to create multiple foreign key constraints of this same table column, one for each of the link domain subclasses which represents a different type of linked item. I know I could have separate columns for the id of each time where the other id columns would be null but this seems kind of messy. If there were a way to just tell GORM I don’t want it to create a foreign key constraint on that column (because different foreign keys use the same column) that would solve the problem.

I know that the question comes up of referential integrity and whether a link key value could be put in the column which does not exist in the foreign table but the application should prevent this situation from occurring.

failing this then I would have to deal with loading the linked item manually and not rely on GORM to do it automatically.

  • 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-02T16:31:28+00:00Added an answer on June 2, 2026 at 4:31 pm

    After a relatively short googling I found Burt Beckwith’s blog entry: http://burtbeckwith.com/blog/?p=465 that explains the basics of GORM customization. With the following configuration class I managed to prevent creation of a key I did not want to get created. In Burt’s example a RootClass is required, but this did not suit my needs so the checking is omitted.

    package com.myapp;
    
    import com.myapp.objects.SomeClass;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration;
    import org.hibernate.MappingException;
    import org.hibernate.mapping.ForeignKey;
    import org.hibernate.mapping.PersistentClass;
    import org.hibernate.mapping.RootClass;
    
    import java.util.Collection;
    import java.util.Iterator;
    
    public class DomainConfiguration extends GrailsAnnotationConfiguration {
        private static final long serialVersionUID = 1;
    
        private boolean _alreadyProcessed;
    
        @SuppressWarnings({"unchecked", "rawtypes"})
        @Override
        protected void secondPassCompile() throws MappingException {
            super.secondPassCompile();
    
            if(_alreadyProcessed) {
                return;
            }
    
            Log log = LogFactory.getLog(DomainConfiguration.class.getName());
    
            for(PersistentClass pc : (Collection<PersistentClass>) classes.values()) {
                boolean preventFkCreation = false;
                String fkReferencedEntityNameToPrevent = null;
    
                if("com.myapp.objects.SomeClassWithUnwantedFkThatHasSomeClassAsAMember".equals(pc.getClassName())) {
                    preventFkCreation = true;
                    fkReferencedEntityNameToPrevent = SomeClass.class.getName();
                }
    
                if(preventFkCreation) {
                    for(Iterator iter = pc.getTable().getForeignKeyIterator(); iter.hasNext(); ) {
                        ForeignKey fk = (ForeignKey) iter.next();
    
                        if(fk.getReferencedEntityName().equals(fkReferencedEntityNameToPrevent)) {
                            iter.remove();
                            log.info("Prevented creation of foreign key referencing " + fkReferencedEntityNameToPrevent + " in " + pc.getClassName() + ".");
                        }
                    }
                }
            }
    
            _alreadyProcessed = true;
        }
    }
    

    The configuration class is introduced to Grails by putting it to datasource.groovy:

    dataSource {
        ...
        ...
        configClass = 'com.myapp.DomainConfiguration
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using spring 3.0.2 for developing a simple web based application like twitter.
I'm developing a web based application built on codeigniter which will also have a
I am developing a web based application in which i have two hyperlinks in
We are developing web based java application. Richfaces used as library.And we have datatable
I'm developing a PHP-based web-application in which you have a form with textarea inputs
We are developing a web based application on glassfish V3. We have 2 application
I'm developing a J2EE-based web application on a SAP Netweaver Application Server. I have
I am developing a small intranet based web application. I have YSlow installed and
I am developing a web based application where I need to have a detachable
I am developing a web application based on JSF technology. I use Eclipse as

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.