不使用domain.delete() 时,Grails 是否会级联删除?

发布于 2024-10-08 10:24:09 字数 523 浏览 7 评论 0原文

来自 Grails 站点: http://www.grails.org/doc/1.0.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html

class Airport {
 String name
 static hasMany = [flights:Flight]
}
class Flight {
 String number
 static belongsTo = [airport:Airport]
}

然后调用 delete() Airport 的实例将删除任何关联的 Flight 对象(因为它们属于机场)。如果我使用 executeUpdate 删除机场,我还能指望它删除航班吗?

谢谢

From the Grails site: http://www.grails.org/doc/1.0.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html

class Airport {
 String name
 static hasMany = [flights:Flight]
}
class Flight {
 String number
 static belongsTo = [airport:Airport]
}

Then calling delete() on an instance of Airport will delete any associated Flight objects (since they belongTo airport). If I were to delete an Airport using executeUpdate can I still expect it to delete the Flights?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅我要高飞 2024-10-15 10:24:09

事实并非如此。下面是一个简单的示例:

 def a0 = new Airport(name: 'Dulles').save()
 def f0 = new Flight(number: '1000', airport: a0).save()

 assert 1 == Airport.count()
 assert 1 == Flight.count()

 Airport.executeUpdate("delete Airport a where a.name = 'Dulles'")

Yields(缩写):

Caused by: java.sql.SQLException: Integrity constraint violation FKB4318470B2E8D1BA table: FLIGHT in statement [delete from airport where name='Dulles']
        at org.hsqldb.jdbc.Util.throwError(Unknown Source)
        at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
        at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
        ... 27 more

存在一个未解决的 Hibernate 问题,要求能够在查询中指定级联 这里

Grails 邮件列表中也对此进行了备份 这里

It does not. Here's a quick example:

 def a0 = new Airport(name: 'Dulles').save()
 def f0 = new Flight(number: '1000', airport: a0).save()

 assert 1 == Airport.count()
 assert 1 == Flight.count()

 Airport.executeUpdate("delete Airport a where a.name = 'Dulles'")

Yields (abbreviated):

Caused by: java.sql.SQLException: Integrity constraint violation FKB4318470B2E8D1BA table: FLIGHT in statement [delete from airport where name='Dulles']
        at org.hsqldb.jdbc.Util.throwError(Unknown Source)
        at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
        at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
        ... 27 more

There's an unresolved Hibernate issue requesting the ability to specify the cascade in the query here.

This is also backed up on the Grails mailing list here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文