groovy grails构建测试数据(buildtestdata插件)如何创建多对多

时间:2013-11-14 00:46:35

标签: mysql hibernate grails

使用Grails 2.2.4。和构建测试数据:2.0.5。我没有使用conf - Bootstrap.groovy在数据库中添加多对多实例。使用MySQL作为数据库。

这是简化的域类:

class Reseller {
String resellerName
static hasMany = [addresses: Address]

}

class Address {
String address1 
static hasMany = [resellers: Reseller]
static belongsTo = [Reseller]   

}

导致在运行时在MySQL中创建...

(下面是一个简单的ER图,因为我无法从MySQL Workbench Diagram发布图像)

经销商==> 1对多==> reseller_address< == many to 1< == address

CREATE TABLE IF NOT NOT EXISTS address(   id bigint(20)NOT NULL AUTO_INCREMENT,   version bigint(20)NOT NULL,   address1 varchar(255)NOT NULL,   主要关键(id) )ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1;

CREATE TABLE IF NOT NOT EXISTS reseller(   id bigint(20)NOT NULL AUTO_INCREMENT,   version bigint(20)NOT NULL,   reseller_name varchar(255)NOT NULL,   主要关键(id) )ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 2;

CREATE TABLE IF NOT NOT EXISTS reseller_addresses(   address_id bigint(20)NOT NULL,   reseller_id bigint(20)NOT NULL,   PRIMARY KEY(reseller_idaddress_id),   KEY FK3F39FB15CB44906Freseller_id),   KEY FK3F39FB1541937EA5address_id) )ENGINE = InnoDB DEFAULT CHARSET = utf8;

ALTER TABLE reseller_addresses   添加约束FK3F39FB1541937EA5外键(address_id)参考addressid),   添加约束FK3F39FB15CB44906F外键(reseller_id)参考resellerid);

这是Bootstrap.groovy:

    import grails.buildtestdata.mixin.Build
import manytomany.*

class BootStrap {

    def init = { servletContext ->
        Address ad1 = Address.build()
        Reseller rs1 = Reseller.build()

        //comment out next line to run and see tables being built.
        rs1.addToAddresses(address: ad1) //does not work.  see error below                      
    }
    def destroy = {
    }
}

以上导致以下错误。它还导致MySQL中的DB表中的零表。我可以看到它们被创建然后它们会在下面的错误中消失。

    | Running Grails application
Hibernate: insert into address (version, address1) values (?, ?)
Hibernate: insert into reseller (version, reseller_name) values (?, ?)
| Error 2013-11-13 17:37:01,543 [localhost-startStop-1] ERROR hibernate.AssertionFailure  - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
Message: null id in manytomany.Address entry (don't flush the Session after an exception occurs)
    Line | Method
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run . . . in java.lang.Thread
| Error 2013-11-13 17:37:01,628 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: null id in manytomany.Address entry (don't flush the Session after an exception occurs)
Message: null id in manytomany.Address entry (don't flush the Session after an exception occurs)
    Line | Method
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run . . . in java.lang.Thread

1 个答案:

答案 0 :(得分:0)

以这种方式尝试,您可以通过.build(...)

将关联传递给父类
def init = { servletContext ->
    Address ad1 = Address.build()
    Reseller rs1 = Reseller.build(addresses:[ad1])
} 

文档:https://github.com/tednaleid/build-test-data/wiki/SampleCode