GORM prevent creation of a foreign key constraint for a Domain

前端 未结 3 1863
一个人的身影
一个人的身影 2021-01-15 13:25

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 fie

3条回答
  •  余生分开走
    2021-01-15 13:47

    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) 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
    }
    

提交回复
热议问题