使用Java从数据库读取UDT的最佳方法是什么? [英] What's the best way to read a UDT from a database with Java?

查看:156
本文介绍了使用Java从数据库读取UDT的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我知道UDT和JDBC的一切,直到有人在 SO 上指出了 java.sql.SQLInput java.sql.SQLData JavaDoc给我。该提示的本质是(来自SQLInput):

I thought I knew everything about UDTs and JDBC until someone on SO pointed out some details of the Javadoc of java.sql.SQLInput and java.sql.SQLData JavaDoc to me. The essence of that hint was (from SQLInput):


一个输入流,包含表示一个实例的值的

SQL结构类型或SQL
不同类型。这个接口只使用
进行自定义映射,后台使用
驱动程序,
程序员永远不会直接调用
SQLInput方法。

An input stream that contains a stream of values representing an instance of an SQL structured type or an SQL distinct type. This interface, used only for custom mapping, is used by the driver behind the scenes, and a programmer never directly invokes SQLInput methods.

这与我以前做的完全相反(当与Oracle JDBC驱动程序一起使用时,它也在生产系统中使用和稳定):实现 SQLData 并在自定义映射中提供此实现

This is quite the opposite of what I am used to do (which is also used and stable in productive systems, when used with the Oracle JDBC driver): Implement SQLData and provide this implementation in a custom mapping to

ResultSet.getObject(int index, Map mapping)

然后JDBC驱动程序将使用<回调我的自定义类型/ p>

The JDBC driver will then call-back on my custom type using the

SQLData.readSQL(SQLInput stream, String typeName)

方法。我实现此方法并从 SQLInput 流中读取每个字段。最后, getObject()将返回一个正确初始化的我的 SQLData 实现的实例,其中包含来自UDT的所有数据。

method. I implement this method and read each field from the SQLInput stream. In the end, getObject() will return a correctly initialised instance of my SQLData implementation holding all data from the UDT.

对我来说,这似乎是实现这种自定义映射的完美方式。采用这种方式的充分理由:

To me, this seems like the perfect way to implement such a custom mapping. Good reasons for going this way:


  • 我可以使用标准API,而不是使用特定于供应商的类,例如 oracle.sql.STRUCT 等。

  • 我可以使用适当的getter / setter和其他属性从我的UDT生成源代码

我的问题


  • 什么你考虑我的方法,实现 SQLData ?即使Javadoc另有说明,它是否可行?

  • 您知道其他什么方式阅读Java中的UDT?例如。春天做什么? Hibernate做了什么? JPA做什么?你做了什么?

  • What do you think about my approach, implementing SQLData? Is it viable, even if the Javadoc states otherwise?
  • What other ways of reading UDT's in Java do you know of? E.g. what does Spring do? what does Hibernate do? What does JPA do? What do you do?

附录

UDT支持和与存储过程的集成是 jOOQ 的主要功能之一。 jOOQ旨在从客户端代码中隐藏更复杂的JDBC事实,而不隐藏底层数据库体系结构。如果您有类似上述类似的问题,jOOQ可能会为您提供答案。

UDT support and integration with stored procedures is one of the major features of jOOQ. jOOQ aims at hiding the more complex "JDBC facts" from client code, without hiding the underlying database architecture. If you have similar questions like the above, jOOQ might provide an answer to you.

推荐答案

配置驱动程序的优势在于它在幕后工作的是程序员不需要将类型映射传递给ResultSet.getObject(...),因此需要记住的细节较少(大部分时间)。还可以在运行时使用属性来配置驱动程序以定义映射,因此应用程序代码可以保持独立于SQL类型到对象映射的详细信息。如果应用程序可以支持多个不同的数据库,则允许为每个数据库支持不同的映射。

The advantage of configuring the driver so that it works behind the scenes is that the programmer does not need to pass the type map into ResultSet.getObject(...) and therefore has one less detail to remember (most of the time). The driver can also be configured at runtime using properties to define the mappings, so the application code can be kept independent of the details of the SQL type to object mappings. If the application could support several different databases, this allows different mappings to be supported for each database.

您的方法是可行的,其主要特征是应用程序代码使用显式类型映射。

Your method is viable, its main characteristic is that the application code uses explicit type mappings.

在幕后方法中,ResultSet.getObject(int)方法将使用连接上定义的类型映射,而不是ResultSet.getObject中应用程序代码传递的类型映射。 (int index,Map mapping)。否则方法是相同的。

In the behind the scenes approach the ResultSet.getObject(int) method will use the type mappings defined on the connection rather than those passed by the application code in ResultSet.getObject(int index, Map mapping). Otherwise the approaches are the same.

其他方法

我见过另一个基于这些类与JBoss 4一起使用的方法:

I have seen another approach used with JBoss 4 based on these classes:

org.jboss.ejb.plugins.cmp.jdbc.JDBCParameterSetter 
org.jboss.ejb.plugins.cmp.jdbc.JDBCResultSetReader.AbstractResultSetReader

这个想法是一样的但实现是非标准的(它可能早于定义SQLData / SQLInput的JDBC标准的版本)。

The idea is the same but the implementation is non-standard (it probably pre-dates the version of the JDBC standard defining SQLData/SQLInput).

这篇关于使用Java从数据库读取UDT的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆