WEB开发网
开发学院软件开发Java 使用 Grails 开发 Google App Engine 应用 阅读

使用 Grails 开发 Google App Engine 应用

 2010-06-24 00:00:00 来源:WEB开发网   
核心提示: 由于在 User 的 Domain Class 中将 OneToOne 的级联(cascade)设置成了 CascadeType.ALL,这样就会在新建的 User 和 UserProfile 时,使用 Grails 开发 Google App Engine 应用(7),将二者放入到同一个实体组

由于在 User 的 Domain Class 中将 OneToOne 的级联(cascade)设置成了 CascadeType.ALL,这样就会在新建的 User 和 UserProfile 时,将二者放入到同一个实体组中,即将 UserProfile 的主键设置成类似于 User(1)/UserProfile(2) 这样的形式,这表明 User 是 UserProfile 的父实体。当删除 User 实体时,会同时将 UserProfile 实体删除。如果 User 和 UserProfile 都变更了,只需保存 User 就能同时将 UserProfile 进行保存。当然,也可以单独对 UserProfile 进行变更存储。但是需要注意的是,对于已经单独持久化的 UserProfile,是没法将其跟一个新增的 User 创建父子关系的。

至于 CascadeType 的设置,可以根据实际项目需求设置成 All、PERSIST、REMOVE、REFRESH、MERGE。

User 和 UserProfile 无主的一对一关系

如果 User 和 UserProfile 是无主的一对一的关系,二者可以单独创建。它们二者之间通过一个 Key 值来进行关联。可以将这个 Key 值看作在两个对象之间建模任意“外键”,但是 GAE 不能保证这些 Key 的引用完整性,所以在使用 Key 的时候,要牢记:

应用程序要确保 Key 值的类型正确

关系双方必须属于同一实体组,才能在同一个事务中对关系双方执行更新

无主关系下的 User/UserProfile 的 DomainClass 参见清单 5、6:

清单 5. User 的 Domain class

 package mulan 
 import javax.persistence.*; 
 @Entity 
 class User implements Serializable { 
  @Id 
 @GeneratedValue(strategy = GenerationType.IDENTITY) 
 Long id  
 Key  uprofile 
…… 
 } 

上一页  2 3 4 5 6 7 8 9 10  下一页

Tags:使用 Grails 开发

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接