我有一个定义Sale
hasMany = [saleComments: SaleComment]
SaleComment域类有一个belongsTo = [sale: Sale]
当我将Sale对象保存在Service方法中而不对salesComment进行任何更改时,GORM正试图插入到sale_comment表中:
Hibernate: insert into line_item (version, amount, date_created, event_id, external_name, fee_rate, fee_rate_type, inclusive, internal_name, label, last_updated, line_item_type, org_id, parent_line_item_id, product_id, sale_id, sale_refund_id, is_tax, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into sale_comment (version, comment, date_created, last_updated, operator_id, sale_id, sale_comments_idx) values (?, ?, ?, ?, ?, ?, ?)
[2014-05-29 23:56:01,205] util.JDBCExceptionReporter Column 'sale_id' cannot be null
[2014-05-29 23:56:01,223] events.PatchedDefaultFlushEventListener Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [com.acme.inventory.domain.SaleComment]
at com.acme.inventory.services.SaleService$_payAndCompleteSale_closure33.doCall(SaleService.groovy:1252)
at org.grails.datastore.gorm.GormStaticApi.withTransaction(GormStaticApi.groovy:687)
at com.acme.inventory.services.SaleService.payAndCompleteSale(SaleService.groovy:1245)
at com.acme.inventory.controllers.SaleController.doPayAndCompleteSale(SaleController.groovy:292)
at com.acme.inventory.controllers.SaleController$_closure3_closure22.doCall(SaleController.groovy:49)
at com.acme.inventory.controllers.BaseController$_closure1.doCall(BaseController.groovy:67)
at com.acme.inventory.controllers.SaleController$_closure3.doCall(SaleController.groovy:48)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'sale_id' cannot be null
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2450)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2371)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2355)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
... 10 more
我不明白为什么GORM试图插入一条尚未被触及的评论。有人有什么想法吗?