Grails GORM: How do I create a composite primary key and use it for a table relationship?

前端 未结 3 620
执念已碎
执念已碎 2021-01-22 01:26

I have two tables, one of which (legacy table: A) has two fields that should serve as a composite foreign key and the other one (new table: B) should use a composite primary key

3条回答
  •  没有蜡笔的小新
    2021-01-22 01:31

    The B class has to be declared Serializable and implements the equals and hashCode methods

    import org.apache.commons.lang.builder.HashCodeBuilder
    class B implements Serializable{
    
        static auditable = true;
    
        String name;
        String className;
        String eventName;
        boolean equals(other) {
        if (!(other instanceof B)) {
            return false
        }
    
        other.className == className && other.eventName == eventName
        }
    
        int hashCode() {
        def builder = new HashCodeBuilder()
        builder.append className
        builder.append eventName
        builder.toHashCode()
        }
    
        static mapping = {
            //supposed to make a composite PK
            id composite:["className", "eventName"] 
        }
    }
    

    and the A class just must to have an attribute of the B class and the GORM make the composite foreign key

    class A {
    
        //composite foreign key to link B class
        B b; //instance of B to be related
    
        static mapping = {
            table 'a_table';            
            id column: 'id';
        }
    }
    

    it create two tables in the database

    +--------------------------------------------------+
    |                       A_TABLE                    |  
    +--------+-----------+--------------+--------------+
    |   id   |  version  |  b_className | b_eventName  |
    +--------+-----------+--------------+--------------+
    --where the Primary key is "id" and the foreign key are "b_className and b_eventName" 
    +--------------------------------------------------+
    |                       B                          |  
    +--------+-----------+--------------+--------------+
    |  name  |  version  |  className   | eventName    |
    +--------+-----------+--------------+--------------+
    --where the Primary key are "className and eventName" 
    

    if you what to change the name of the columns to other just add the clausule in the mapping statement

    class A {
    
        //composite foreign key to link B class
        B b; //instance of B to be related
    
        static mapping = {
            table 'a_table';            
            id column: 'id';
            columns {
                  b {
                     column name: "className"
                     column name: "eventName"
                  }
            }
        }
    }
    

提交回复
热议问题