GAE + JDO搭建商业网站

 

 

转载请标明原文地址:



Java 数据存储区索引配置

要使用索引服务,需要这个配置。不过gae会自动配置。有2xml文件定义这个配置:

 1. 手动配置 WEB-INF/ 目录下创建一个 datastore-indexes.xml 文件,例子如下

 

 

<datastore-indexes> 元素具有 autoGenerate 属性,该属性控制是否应将此文件与自动生成的索引配置一同考虑。请参阅下面的使用自动索引配置

每个 <datastore-index> 元素表示一个索引。kind 属性指定要编入索引的实体类型。如果索引支持通过父实体组过滤实体的查询,则 ancestor 属性为 true ,否则为 false

<datastore-index> 中的 <property> 元素代表要编入索引的实体属性name 属性是属性名称,direction 属性是排序顺序(对于升序为 asc ,对于降序为 desc )。属性元素的顺序将指定索引中的顺序:行首先按第一个属性排序,然后按第二个属性排序,以此类推。


2. 使用自动索引配置

 

手动确定应用程序查询所需的索引非常繁琐且易于出错。幸运的是,开发服务器可为您确定索引配置。要使用自动索引配置,请将 autoGenerate="true" 属性添加到 datastore-indexes.xml 文件的 <datastore-indexes> 元素中。如果您的应用程序没有 datastore-indexes.xml 文件,则也将使用自动索引配置。

启用自动索引配置后,开发服务器将在应用程序 WAR 中的 WEB-INF/appengine-generated/ 目录下维护一个名为 datastore-indexes-auto.xml 的文件。如果在开发服务器中运行的应用程序试图进行数据存储区查询,而该查询在 datastore-indexes.xmldatastore-indexes-auto.xml 中都没有对应的索引,则服务器将向 datastore-indexes-auto.xml 添加适当的配置。

如果在您上传应用程序时自动索引配置已启用,则 AppCfg 将使用 datastore-indexes.xmldatastore-indexes-auto.xml 来确定在 App Engine 上需要为应用程序构建的索引。

如果您的 datastore-indexes.xml 中出现 autoGenerate="false" ,则开发服务器和 AppCfg 将忽略 datastore-indexes-auto.xml 的内容。如果在开发服务器中运行的应用程序执行查询,而查询的索引未在 datastore-indexes.xml 中指定,则开发服务器将如同 App Engine 一样引发异常。

最好不定期地将索引配置从 datastore-indexes-auto.xml 移动到 datastore-indexes.xml 中,然后禁用自动索引配置并在开发服务器上测试您的应用程序。这样做使得不必管理两个文件即可轻松维护索引,并确保您的测试可重现因缺失索引配置而导致的错误。

 

 


3.事务

 

数据存储区对单个事务中可完成的功能施加了许多限制。

事务中的所有数据存储区操作必须在同一实体组中的实体上进行 。这包括通过键、更新实体和删除实体来检索实体。请注意,每个根实体都属于单独的实体组,因此,单个事务不能创建多个根实体或在多个根实体上进行操作

应用程序在事务过程中不能执行查询。但是,应用程序可以在事务过程中使用键检索数据存储区实体,并保证抓取的实体与事务的其余实体一致。您可以在事务之前准备键,或者在事务内部根据键名或 ID 生成键。

应用程序不能在单个事务中多次创建或更新实体。

JDO 将在单个事务中执行调用 tx.begin() 与调用 tx.commit() 之间的所有操作。如果某个操作因所请求的实体组正在被其他进程使用而失败,则 JDO 将引发 JDODataStoreException 或 JDOException,由 java.util.ConcurrentModificationException 引起。

 

Exemple:

 

 

 


JDO 4种如何处理父子类关系的方式

 

In Java it is a normal situation to have inheritance between classes. With JDO you have choices to make as to how you want to persist your classes for the inheritance tree. For each class you select how you want to persist that classes information. You have the following choices.[3]

  1. The first and simplest to understand option is where each class has its own table in the datastore. In JDO2 this is referred to as new-table .
  2. The second way is to select a class to have its fields persisted in the table of its subclass. In JDO2 this is referred to as subclass-table
  3. The third way is to select a class to have its fields persisted in the table of its superclass. In JDO2 this is known as superclass-table .
  4. A DataNucleus extension way is to have all classes in an inheritance tree with their own table containing all fields. This is known as complete-table and is enabled by setting the inheritance strategy of the root class to use this.

例子:[4]

 

 

 

In this example we've added an @Inheritance annotation to the Worker class declaration with its <strategy> attribute set to InheritanceStrategy.SUBCLASS_TABLE . This tells JDO to store all persistent fields of the Worker in the datastore entities of its subclasses. The datastore entity created as the result of calling makePersistent() with an Employee instance will have two properties named "department" and "salary". The datastore entity created as the result of calling makePersistent() with an Intern instance will have two properties named "department" and "internshipEndDate". There will not be any entities of kind "Worker" in the datastore.

 

 

 

总结:

 * JDO 保存数据在一个文件中(可以重复保存不同键的同一个数据,所以是持久的(服务器重启后数据仍然存在

                  - JDO enregistre les BDD dans un fichier (on peut avoir plusier fois meme donnee)

 * JDO 每次使用完一个PersistenceManager pm 后,必须用 close 关闭它,否则数据不保存到数据库。 但是close后pm无法再使用。

 * 对于键的问题: 如果实体无父实体,则可以用 Long id; 否则不能使用Long ,一般用 Key[2]

                  JDO, un bean n'a pas de pere => Long id comme key
                           un bean a de pere => Key comme key

 * 对于qury.execute 返回的是一个列表 所以一般用 list 保存返回值
                   - JDO, query.execute return un list, mais pas d'une instance

 

* 为了可以通过键找到相应对象,尽量自己给对象设置键(合理逻辑地组织数据形式),而不是让系统设置

* 如果子实体与父实体都是Key类型作为键,那他们的参数不能相同(不是很确定)

 

 

参考网站:

1. google app engine官网:

 http://code.google.com/intl/zh-CN/appengine/docs/

 

[2] http://code.google.com/intl/zh-CN/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Creating_and_Using_Keys

 

[3] http://www.datanucleus.org/products/accessplatform/jdo/orm/inheritance.html

[4] http://code.google.com/intl/fr/appengine/docs/java/datastore/dataclasses.html#Inheritance

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值