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

前端 未结 3 595
执念已碎
执念已碎 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"
                  }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-22 01:32

    Did you try to use attribute name instead of use attribute value ?

    class B implements Serializable{
        String name;
        String className;
        String eventName;
    
        static mapping = {
            //supposed to make a composite PK
            id composite:['className', 'eventName'] 
        }
    }
    

    And mapping in A :

    class A {
        static hasMany = [ b : B ]
    }
    

    No need to have className or eventName in A

    0 讨论(0)
  • 2021-01-22 01:36
    import org.apache.commons.lang.builder.HashCodeBuilder
    class Person implements Serializable {
    
    String firstName
    String lastName
    
    boolean equals(other) {
        if (!(other instanceof Person)) {
            return false
        }
    
        other.firstName == firstName && other.lastName == lastName
    }
    
    int hashCode() {
        def builder = new HashCodeBuilder()
        builder.append firstName
        builder.append lastName
        builder.toHashCode()
    }
    
    static mapping = {
        id composite: ['firstName', 'lastName']
    }
    }
    

    this is what you can find from official docs of grails,

    http://grails.org/doc/latest/guide/GORM.html#compositePrimaryKeys

    just blindly follow this and your problem will be solved. for explanation refer above link

    0 讨论(0)
提交回复
热议问题