简体   繁体   English

java.sql.SQLData-Oracle对象映射问题

[英]java.sql.SQLData - Oracle object mapping problem

I am using java.sql.SQLData interface to map my java objects to Oracle database types. 我正在使用java.sql.SQLData接口将Java对象映射到Oracle数据库类型。

For example, I have an object type Person in Oracle DB defined as: 例如,我在Oracle DB中将对象类型Person定义为:

CREATE OR REPLACE TYPE PERSON AS OBJECT
(
      PERSON_ID NUMBER,
      PERSON_NAME VARCHAR2(100)
);

Corresponding Java type is: 对应的Java类型是:

public class Person implements SQLData {

 private String sql_type = "PERSON";
 private int personId;
 private String personName;

 public int getPersonId() {
  return personId;
 }

 public void setPersonId(int personId) {
  this.personId = personId;
 }

 public String getPersonName() {
  return personName;
 }


 public void setPersonName(String personName) {
    this.personName = personName;
 }

 public void readSQL(SQLInput stream, String typeName) throws SQLException 
 {
     this.sql_type=typeName;
     this.personId = stream.readLong();
     this.personName = stream.readString();
 }

 public void writeSQL(SQLOutput stream) throws SQLException 
 {
       stream.writeLong(this.personId);
        stream.writeString(this.personName);
 }
}

This works fine currently and populates Person Objects from database type. 当前,此方法工作正常,并从数据库类型填充“人员对象”。

Now, I have a another type and it's corresponding collection as follows: 现在,我有另一个类型,它的对应集合如下:

CREATE OR REPLACE TYPE SUBJECT AS OBJECT
    (
          SUBJECT_ID NUMBER,
          SUBJECT_NAME VARCHAR2(100)
    );
 -- Corresponding List
 CREATE OR REPLACE TYPE SUBJECT_LIST IS TABLE OF SUBJECT;

I have to create a new entry in type PERSON with this collection as follows: 我必须使用此集合在PERSON类型中创建一个新条目,如下所示:

CREATE OR REPLACE TYPE PERSON AS OBJECT
(
      PERSON_ID NUMBER,
      PERSON_NAME VARCHAR2(100),
      SUBJECT_LIST TYPE SUBJECT_LIST
);

To make this change I have to change my java Person class. 要进行此更改,我必须更改我的Java Person类。 I tried adding java.sql.Array parameter but it is not working. 我尝试添加java.sql.Array参数,但无法正常工作。

Can you please help here to map the new PERSON Object type to Java type? 您能否在这里帮助将新的PERSON Object类型映射为Java类型?

Thanks in advance. 提前致谢。

--Siddharth --Siddharth

The documentation of SQLInput (link below) has this on the first line... SQLInput的文档(下面的链接)在第一行中具有此功能...

"This interface [ie SQLInput] ... is used by the driver behind the scenes, and a programmer never directly invokes SQLInput methods." “该接口[即SQLInput] ...由幕后的驱动程序使用,程序员从不直接调用SQLInput方法。”

Are you sure you should be using SQLInput directly? 确定要直接使用SQLInput吗? Is there an example you're following? 有没有您要关注的示例?

Ref: http://java.sun.com/j2se/1.4.2/docs/api/java/sql/SQLInput.html#readObject%28%29 参考: http : //java.sun.com/j2se/1.4.2/docs/api/java/sql/SQLInput.html#readObject%28%29

Hey I did not access this so sorry I could not reply. 嘿,我没有访问此邮件,对不起,我无法回复。 What I did was to have a java.sql.Array defined in my class. 我所做的是在类中定义了java.sql.Array。 This maps to the the nested type in Oracle database. 这映射到Oracle数据库中的嵌套类型。 So in this case my Person class will have an instance variable : 因此,在这种情况下,我的Person类将具有一个实例变量:

java.sql.Array subjectList;

To set the value you will need to do the following: 要设置该值,您需要执行以下操作:

Subject[] subjectListArray=null;
Person p = new Person();
p.setSubjectList(new oracle.sql.ARRAY(getOracleArray(typeName, connection, subjectListArray)));

The getOracleArray method will be something like this: getOracleArray方法将如下所示:

public static oracle.sql.ArrayDescriptor getOracleArray(final String typeName)throws SQLException
{
    if(typeName==null)return null;
    final oracle.sql.ArrayDescriptor arrayDescriptor = new oracle.sql.ArrayDescriptor(
            typeName, con);
    return arrayDescriptor;
}

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

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