我有以下设置。
类,比方说,有CarPart的汽车(belongsTo = [car:Car])。
当我创建汽车时,我也想创建som默认的CarParts,所以我做
def car = new Car(bla bla bla)
def part = new CarPart(car:car)
现在,当我执行car.validate()或part.validate()时,它看起来很好。 但是当我这样做时(car.save&& part.save()我得到了这个例外:
2012-03-24 14:02:21,943 [http-8080-4] ERROR util.JDBCExceptionReporter - Batch entry 0 insert into car_part (version, car_id, id) values ('0', '297', '298') was aborted. Call getNextException to see the cause.
2012-03-24 14:02:21,943 [http-8080-4] ERROR util.JDBCExceptionReporter - ERROR: value too long for type character varying(6)
2012-03-24 14:02:21,943 [http-8080-4] ERROR events.PatchedDefaultFlushEventListener - Could not synchronize database state with session
org.hibernate.exception.DataException: Could not execute JDBC batch update
Stacktrace follows:
java.sql.BatchUpdateException: Batch entry 0 insert into car_part (version, deal_id, id) values ('0', '297', '298') was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2621)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1837)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2754)
at $Proxy20.flush(Unknown Source)
at ristretto.DealController$_closure5.doCall(DealController.groovy:109)
at ristretto.DealController$_closure5.doCall(DealController.groovy)
at java.lang.Thread.run(Thread.java:722)
有什么想法吗?
答案 0 :(得分:2)
调用validate实际上只是检查约束。保存失败的是数据库的实际持久性(验证无法真正检查)。
通常,在Grails中,您可能希望通过以下两种方式之一执行此操作:
def car = new Car(bla bla)
car.save()
def carPart = new CarPart(car:car)
carPart.save()
或
def car = new Car(bla bla)
def carPart = new CarPart(bla bla)
car.addToCarParts(carPart)
car.save()