在应用程序中将OJB作为一个存储层使用(三)
2003-09-23 01:33
573 查看
如果在product表中有10000条记录,那么从存储库中获得所有得记录是一个很费时的操
作,每个记录都必须新建一个对象,整个表都要读入内存。在示例程序中,没有考虑性
能问题,但是在一个实际的应用OJB的程序中,我们需要一个更有效的方法来获得所有记
录,如果你的程序不需要将整个表调入内存,那么建议你使用getIteratorByQuery()方
法来返回Iterator而不返回Collection。
如果你需要遍历较大的结果集,这种方法很实用。所有实例并不是一次性被创建,只是
在你"需要"的时候才分配内存。用户不再使用的存储结构实例可以被garbage collecto
r重新回收。下面就是使用这种方法的实例代码:
public void apply()
{
System.out.println("The list of available products:");
// build a query that select all objects of Class Product,
// without any further criteria according to ODMG
// the Collection containing all
// instances of a persistent class is called "Extent"
Query query = new QueryByCriteria(Product.class, null);
try
{
// ask the broker to retrieve an Iterator
java.util.Iterator iter = broker.getIteratorByQuery(query);
// now iterate over the result to print each product
while (iter.hasNext())
{
System.out.println(iter.next());
}
}
catch (Throwable t)
{
t.printStackTrace();
}
}
更详细的说明可以参考PersistenceBroker JavaDoc和Query Doc。
存储对象:
现在让我们来看看UCEnterNewProduct类。它是这样来工作的:首先,它创建一个新的对
象,然后要求用户输入新产品的相关数据(产品名称,价格,库存量)。这些数据被存
储在新对象中。然后我们必须把新创建的对象存到存储库中。我们可以使用Persistenc
eBroker.store(Object ojb):
public void apply()
{
// this will be our new object
Product newProduct = new Product();
// now read in all relevant information and fill the new object:
System.out.println("please enter a new product");
String in = readLineWithMessage("enter name:");
newProduct.setName(in);
in = readLineWithMessage("enter price:");
newProduct.setPrice(Double.parseDouble(in));
in = readLineWithMessage("enter available stock:");
newProduct.setStock(Integer.parseInt(in));
// now perform persistence operations
try
{
// 1. open transaction
broker.beginTransaction();
// 2. make the new object persistent
broker.store(newProduct);
broker.commitTransaction();
}
catch (PersistenceBrokerException ex)
{
// if something went wrong: rollback
broker.abortTransaction();
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
可能你已经发现了,我们并没有新产品的主键id赋值。OJB能够检查到一个新产品的id没
有被设置而自动制定一个唯一的id值。Id值的自动增长是在Repository-XML中定义的。
更新对象:
当用户相编辑一个产品的时候(通过从目录中选择2),用户必须输入想编辑产品的id值
。因为程序没有维护着产品对象表,系统必须首先通过PersistenceBroker从存储库中得
到一个产品。
通过PersistenceBroker选择一个对象很简单――我们必须首先创建一个QueryByCriter
ia对象。QueryByCriteria对象含有用户输入的id值信息,你可能发祥我们并不需要产品
的其他信息,我们只需要对productId设置过滤器就行了。直接构建一个Criteria对象可
以让你申明一个复杂的条件如productId值必须大于2小于5。复杂的查询在本文的后面将
介绍。
一旦通过broker.getObjectByQuery(query)方法获得了Product对象,接下来就能够通过
用户输入来修改对象属性,然后通过broker.store(toBeEdited)将修改后的结构存入存
储库。下面是UCEditProduct类的有关代码:
public void apply()
{
String in = readLineWithMessage("Edit Product with id:");
int id = Integer.parseInt(in);
// We do not have a reference to the selected Product.
// So first we have to lookup the object,
// we do this by a query by example (QBE):
// 1. build an example object with matching primary key values:
Product example = new Product();
example.setId(id);
// 2. build a QueryByCriteria from this sample instance:
Query query = new QueryByCriteria(example);
try
{
// 3. start broker transaction
broker.beginTransaction();
// 4. lookup the product specified by the QBE
Product toBeEdited = (Product) broker.getObjectByQuery(query);
// 5. edit the existing entry
System.out.println("please edit the product entry");
in = readLineWithMessage(
"enter name (was " + toBeEdited.getName() + "):");
toBeEdited.setName(in);
in = readLineWithMessage(
"enter price (was " + toBeEdited.getPrice() + "):");
toBeEdited.setPrice(Double.parseDouble(in));
in = readLineWithMessage(
"enter available stock (was " +
toBeEdited.getStock()+ "):");
toBeEdited.setStock(Integer.parseInt(in));
// 6. now ask broker to store the edited object
broker.store(toBeEdited);
// 7. commit transaction
broker.commitTransaction();
}
catch (Throwable t)
{
// rollback in case of errors
broker.abortTransaction();
t.printStackTrace();
}
}
作,每个记录都必须新建一个对象,整个表都要读入内存。在示例程序中,没有考虑性
能问题,但是在一个实际的应用OJB的程序中,我们需要一个更有效的方法来获得所有记
录,如果你的程序不需要将整个表调入内存,那么建议你使用getIteratorByQuery()方
法来返回Iterator而不返回Collection。
如果你需要遍历较大的结果集,这种方法很实用。所有实例并不是一次性被创建,只是
在你"需要"的时候才分配内存。用户不再使用的存储结构实例可以被garbage collecto
r重新回收。下面就是使用这种方法的实例代码:
public void apply()
{
System.out.println("The list of available products:");
// build a query that select all objects of Class Product,
// without any further criteria according to ODMG
// the Collection containing all
// instances of a persistent class is called "Extent"
Query query = new QueryByCriteria(Product.class, null);
try
{
// ask the broker to retrieve an Iterator
java.util.Iterator iter = broker.getIteratorByQuery(query);
// now iterate over the result to print each product
while (iter.hasNext())
{
System.out.println(iter.next());
}
}
catch (Throwable t)
{
t.printStackTrace();
}
}
更详细的说明可以参考PersistenceBroker JavaDoc和Query Doc。
存储对象:
现在让我们来看看UCEnterNewProduct类。它是这样来工作的:首先,它创建一个新的对
象,然后要求用户输入新产品的相关数据(产品名称,价格,库存量)。这些数据被存
储在新对象中。然后我们必须把新创建的对象存到存储库中。我们可以使用Persistenc
eBroker.store(Object ojb):
public void apply()
{
// this will be our new object
Product newProduct = new Product();
// now read in all relevant information and fill the new object:
System.out.println("please enter a new product");
String in = readLineWithMessage("enter name:");
newProduct.setName(in);
in = readLineWithMessage("enter price:");
newProduct.setPrice(Double.parseDouble(in));
in = readLineWithMessage("enter available stock:");
newProduct.setStock(Integer.parseInt(in));
// now perform persistence operations
try
{
// 1. open transaction
broker.beginTransaction();
// 2. make the new object persistent
broker.store(newProduct);
broker.commitTransaction();
}
catch (PersistenceBrokerException ex)
{
// if something went wrong: rollback
broker.abortTransaction();
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
可能你已经发现了,我们并没有新产品的主键id赋值。OJB能够检查到一个新产品的id没
有被设置而自动制定一个唯一的id值。Id值的自动增长是在Repository-XML中定义的。
更新对象:
当用户相编辑一个产品的时候(通过从目录中选择2),用户必须输入想编辑产品的id值
。因为程序没有维护着产品对象表,系统必须首先通过PersistenceBroker从存储库中得
到一个产品。
通过PersistenceBroker选择一个对象很简单――我们必须首先创建一个QueryByCriter
ia对象。QueryByCriteria对象含有用户输入的id值信息,你可能发祥我们并不需要产品
的其他信息,我们只需要对productId设置过滤器就行了。直接构建一个Criteria对象可
以让你申明一个复杂的条件如productId值必须大于2小于5。复杂的查询在本文的后面将
介绍。
一旦通过broker.getObjectByQuery(query)方法获得了Product对象,接下来就能够通过
用户输入来修改对象属性,然后通过broker.store(toBeEdited)将修改后的结构存入存
储库。下面是UCEditProduct类的有关代码:
public void apply()
{
String in = readLineWithMessage("Edit Product with id:");
int id = Integer.parseInt(in);
// We do not have a reference to the selected Product.
// So first we have to lookup the object,
// we do this by a query by example (QBE):
// 1. build an example object with matching primary key values:
Product example = new Product();
example.setId(id);
// 2. build a QueryByCriteria from this sample instance:
Query query = new QueryByCriteria(example);
try
{
// 3. start broker transaction
broker.beginTransaction();
// 4. lookup the product specified by the QBE
Product toBeEdited = (Product) broker.getObjectByQuery(query);
// 5. edit the existing entry
System.out.println("please edit the product entry");
in = readLineWithMessage(
"enter name (was " + toBeEdited.getName() + "):");
toBeEdited.setName(in);
in = readLineWithMessage(
"enter price (was " + toBeEdited.getPrice() + "):");
toBeEdited.setPrice(Double.parseDouble(in));
in = readLineWithMessage(
"enter available stock (was " +
toBeEdited.getStock()+ "):");
toBeEdited.setStock(Integer.parseInt(in));
// 6. now ask broker to store the edited object
broker.store(toBeEdited);
// 7. commit transaction
broker.commitTransaction();
}
catch (Throwable t)
{
// rollback in case of errors
broker.abortTransaction();
t.printStackTrace();
}
}
相关文章推荐
- 在应用程序中将OJB作为一个存储层使用(一)
- 在应用程序中将OJB作为一个存储层使用(五)
- 在应用程序中将OJB作为一个存储层使用(六)
- 在应用程序中将OJB作为一个存储层使用(二)
- 在应用程序中将OJB作为一个存储层使用(四)
- 使用一个窗体应用程序作为WCF服务的宿主
- 使用CubeMx制作一个SD卡作为存储空间的U盘
- 历年世界杯冠军队伍如下,使用map存储这些队伍,使用年份作为key,冠军队伍名称作为value。 完成下面的功能: 1) 输入一个年份,如果当年有世界杯比赛,则输出获奖队伍,如果没有则输出“当年没有举
- 66.6 设计一个雇员类 employee,存储雇员的姓名、编号和生日等信息,要求该类使用 上一题设计的日期类作为成员对象。雇员类的使用如下: //定义一个雇员,其雇员号为 10,生日为 1970 年
- 开发和部署一个作为 WebSphere Portal V5 Portlet 的 Struts 应用程序
- 使用CodeSmith为一个ORACLE中的表生成存储过程失败
- MOSS 2007 入门(6) : 使用一个Code-behind的用户控件作为WebPart
- (WebSite----Asp.Net Configuration----->无法连接到SQL Server数据库------>选择数据存储区---->应用程序当前被配置为使用提供程序:AspNetSqlProvider)解决方案
- 关于存储过程中一个参数表示多个查询条件使用方法的简单处理
- 一个项目中正在使用的SQL分页存储过程
- 使用注册表键存储应用程序信息
- 一个查看MSSQLServer数据库空间使用情况的存储过程 SpaceUsed
- JAVA使用XML作为持久存储介质实现方法
- 如何使用C#创建一个三层的数据库应用程序
- GROUP BY的另种使用方法:将结果集一列的内容作为一个单元格显示