Quantcast
Channel: Save Your Knowledge
Viewing all articles
Browse latest Browse all 9

Map Oracle to Java Objects

$
0
0

Oracle Objects are user defined types that are used to insert some OOP elements in a well-known relational database.

Sometimes does happen that you need to map a more complex behaviour and standard types can’t give you all the instruments to do this or, at least, make the operation harder.

For example, suppose that you need to manage a database with employees information. In the personal information table you may have some address data like: street name, city, postal code ,etc. It can be simpler to map these informationto a specific ADDRESS object that can be inserted in a table like a simple data type.

Let’s see the differences:

This is an example of employee table created without Oracle Types


Create table employee (

idEmployee number(5),

name varchar2(20),

sal number(10,2),

city varchar(20),

postalCode varchar(5),

streetName varchar(50),

streetNum number(5)

);

Now let’s see the same information coded with Oracle Types :


Create type address as object (

city varchar(20),

postalCode varchar(5),

streetName varchar(50),

streetNum number(5)

)

Now we can create employee table using objects just created:


create table employee(

name varchar2(20),

sal number(10,2),

empAddress address

);

As you can see we now use the object “address” as a normal type. We can access to “address” fields using the canonical notation. For example to get city name we just write empAddress.city .

The explaining of Oracle Types is not the focus of this article and if you want to learn more about this you can read Oracle’s Introduction to Oracle Objects .

It’s simple to map Java primitive types with Oracle just using jdbc drivers.

But what about mapping an Oracle Object Type with a Java Type?

You need to create a Java Object that have same fields of Oracle Type and that implements the interface java.slq.Sqldata that provide some methods to implements to provide mapping between the two worlds.

You have to implement three methods that will be called from ORACLE at the mapping time:

1)public String getSqlTypeName()  that returns the Oracle name of the object

2)public void readSql(SQLInput stream,String typename) that gives information how to map Oracle Object to Java Object. You have to set java fields with fields read from SQLInput object, gotten sequencelly in the same order of Oracle Type specification

3)public void writeSQL(SQLOutput  stream) that gives information how to map Java Object to Oracle Type. You have to set Oracle Type fields using SQLOutput object, putting them sequencelly in the same order of Oracle Type specification.


public class Employee implements SQLData {

private String city;

private String postalCode;

private String  streetName;

private int streetNum;

public String getSQLTypeName() throws SQLException {

return "Address";

}

public void readSQL(SQLInput stream, String typeName) throws SQLException {

//get City

setCity(stream.readString());

//get Postal Code

setPostalCode(stream.readString());

//get Street Name

setStreetName(stream.readString());

//get StreetNum

setStreetNum(stream.readInt());

}

public void writeSQL(SQLOutput stream) throws SQLException {

//write City

stream.writeString(getCity());

//write Postal Code

stream.writeString(getPostalCode());

//write Street Name

stream.writeString(getStreetName());

//write StreeNum

stream.writeInt(getStreetNum());

}

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

public String getPostalCode() {

return postalCode;

}

public void setPostalCode(String postalCode) {

this.postalCode = postalCode;

}

public String getStreetName() {

return streetName;

}

public void setStreetName(String streetName) {

this.streetName = streetName;

}

public int getStreetNum() {

return streetNum;

}

public void setStreetNum(int streetNum) {

this.streetNum = streetNum;

}

}


Now you can call Object Address as other Oracle Types with jdbc, using OracleTypes.STRUCT as jdbc type when you put it in a statement.

Viewing all articles
Browse latest Browse all 9

Trending Articles