简体   繁体   English

Oracle存储过程可以返回Java中可用的对象吗?

[英]Can an Oracle stored procedure return an object useable in Java?

I am working with a PL/SQL developer on a project. 我正在与一个项目的PL / SQL开发人员一起工作。 The other developer is writing the stored procedures I need to call. 另一个开发人员正在编写我需要调用的存储过程。

One of the stored procedures needs to return everything about a product: 存储过程之一需要返回有关产品的所有信息:

name, price, description, color, weight, manufacturer, etc - ( 15 pieces of info in total ) 名称,价格,描述,颜色,重量,制造商等-(共15条信息)

I assumed that the stored procedure would accept one IN parameter, the product-id and return 15 OUT parameters. 我假设存储过程将接受一个I​​N参数,product-id并返回15个OUT参数。

Is there a way that the stored procedure can return an object that I can cast to a custom datatype? 有没有一种方法可以使存储过程返回可以转换为自定义数据类型的对象? Is this a typical technique, or would you normally return multiple OUT parameters? 这是一种典型的技术,还是您通常会返回多个OUT参数?

My preference would be that the procedure (or if it is just retrieving the product information, a function) returned a PL/SQL product object that the Java code retrieves either as a java.sql.STRUCT or as a more strongly typed object. 我的偏爱是该过程(或者,如果只是检索产品信息,则是一个函数)返回了一个PL / SQL产品对象,Java代码将其作为java.sql.STRUCT或更强类型的对象来检索。

Oracle has sample code for both approaches on its web site. Oracle在其网站上提供了这两种方法的示例代码。 Here is an example of the java.sql.STRUCT approach . 这是java.sql.STRUCT方法的示例。 And here is the strongly typed object approach . 这是强类型对象方法 The strongly typed example uses JPublisher to create the strongly typed object classes but the JDBC Developer's Guide walks you through creating your own mappings. 强类型示例使用JPublisher创建强类型对象类,但是《 JDBC开发人员指南》将引导您创建自己的映射。

Hiberante provides a way to map the result of a sql query or a stored procedure to a java object. Hiberante 提供了一种将sql查询或存储过程的结果映射到java对象的方法。 It is a complex Object-Relational Mapping solution, but in most cases it's worth the effort. 它是一个复杂的对象关系映射解决方案,但是在大多数情况下,这是值得的。

SQL查询不能以任何方式返回对象,您应该只返回选择运算符的结果,然后使用该结果自己构造一个对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM