使用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 <==多个到1 <==地址
CREATE TABLE IF NOT NOT EXISTS address
( id
bigint(20)NOT NULL AUTO_INCREMENT, version
bigint(20)NOT NULL, address1
varchar(255)NOT NULL,PRIMARY KEY( 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,PRIMARY KEY( 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_id
, address_id
),KEY FK3F39FB15CB44906F
( reseller_id
),KEY FK3F39FB1541937EA5
( address_id
))ENGINE = InnoDB DEFAULT CHARSET = utf8 ;
ALTER TABLE reseller_addresses
ADD CONSTRAINT FK3F39FB1541937EA5
FOREIGN KEY( address_id
)REFERENCES address
( id
),ADD CONSTRAINT FK3F39FB15CB44906F
FOREIGN KEY( reseller_id
)REFERENCES reseller
( id
);
这是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 回答
以这种方式尝试,您可以通过
.build(...)
将关联传递给父类:文件:https://github.com/tednaleid/build-test-data/wiki/SampleCode