在GORM中重命名复合外键

在GORM中重命名复合外键

本文介绍了在GORM中重命名复合外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几类:

  
class Catalog {
static mapping = {
id composite:['name','manufacturer']
列{
名字栏:'cat_name'
制造商专栏:'manuf_id'
}
}
字符串名称
制造商制造商
}

类别顺序{
static mapping = {
列表{
//如何将外键重命名为cat_name,manuf_id?
}
}
目录目录//创建catalog_name,catalog_manufacturer_name
}

目前,使用属性catalog_name和catalog_manufacturer_name(它引用Catalog表的复合主键)生成Order表。



我需要将这些生成的列重命名为Order表中的cat_name和manuf_id以使用现有数据库。这是可能的,如果是这样,如何?

解决方案

使用GORM配置是不可能的,但您可以使用自定义配置类:

  package com.foo.bar; 

import java.util.Collection;
import java.util.Iterator;

导入org.codehaus.groovy.grails.orm.hibernate.cfg.DefaultGrailsDomainConfiguration;
import org.hibernate.MappingException;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;

public class CompositeAwareHibernateConfiguration扩展DefaultGrailsDomainConfiguration {

private static final long serialVersionUID = 1;

私人布尔_alreadyProcessed;

@SuppressWarnings(unchecked)
@Override
protected void secondPassCompile()抛出MappingException {
super.secondPassCompile();

if(_alreadyProcessed){
return; (PersistentClass pc:(Collection< PersistentClass>)classes.values()){
if(pc instanceof RootClass){
RootClass root =(


) RootClass)件; ();
if(com.foo.bar.Order.equals(root.getClassName())){
for(Iterator iter = root.getTable()。getColumnIterator(); iter.hasNext() ;){
列的列=(列)iter.next();
if(catalog_name.equals(column.getName())){
column.setName(cat_name);
}
else if(catalog_manufacturer_id.equals(column.getName())){
column.setName(manuf_id);
}
}
}
}
}

_alreadyProcessed = true;




$ b将类放在src / java中并注册它DataSource.groovy:

  dataSource {
pooled = true
driverClassName = ...
username = ...
password = ...
configClass = com.foo.bar.CompositeAwareHibernateConfiguration
}


I have the following classes:


class Catalog {
  static mapping = {
    id composite:['name', 'manufacturer']
    columns {
      name column:'cat_name'
      manufacturer column:'manuf_id'
    }
  }
  String name
  Manufacturer manufacturer
}

class Order {
  static mapping = {
    columns {
      // How to rename foreign keys as cat_name, manuf_id?
    }
  }
  Catalog catalog // creates catalog_name, catalog_manufacturer_name
}

Presently, an Order table is generated with the attributes catalog_name and catalog_manufacturer_name (which reference the composite primary keys of the Catalog table).

I need to rename these generated columns to cat_name and manuf_id in the Order table to work with an existing database. Is this possible, and if so, how?

解决方案

It's not possible using GORM configuration, but you can do it with a custom Configuration class:

package com.foo.bar;

import java.util.Collection;
import java.util.Iterator;

import org.codehaus.groovy.grails.orm.hibernate.cfg.DefaultGrailsDomainConfiguration;
import org.hibernate.MappingException;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;

public class CompositeAwareHibernateConfiguration extends DefaultGrailsDomainConfiguration {

   private static final long serialVersionUID = 1;

   private boolean _alreadyProcessed;

   @SuppressWarnings("unchecked")
   @Override
   protected void secondPassCompile() throws MappingException {
      super.secondPassCompile();

      if (_alreadyProcessed) {
         return;
      }

      for (PersistentClass pc : (Collection<PersistentClass>)classes.values()) {
         if (pc instanceof RootClass) {
            RootClass root = (RootClass)pc;
            if ("com.foo.bar.Order".equals(root.getClassName())) {
               for (Iterator iter = root.getTable().getColumnIterator(); iter.hasNext(); ) {
                  Column column = (Column)iter.next();
                  if ("catalog_name".equals(column.getName())) {
                     column.setName("cat_name");
                  }
                  else if ("catalog_manufacturer_id".equals(column.getName())) {
                     column.setName("manuf_id");
                  }
               }
            }
         }
      }

      _alreadyProcessed = true;
   }
}

Put the class in src/java and register it in DataSource.groovy:

dataSource {
   pooled = true
   driverClassName = ...
   username = ...
   password = ...
   configClass = com.foo.bar.CompositeAwareHibernateConfiguration
}

这篇关于在GORM中重命名复合外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:54