• Entries (RSS)
  • Comments (RSS)

Oracle CLOB data type and Entity Beans in WebSphere Commerce Server

Posted by | Posted in WebSphere Commerce | Posted on 06-04-2009

Tagged Under : , ,

Oracle CLOB data type and Entity Beans in WebSphere Commerce Server.

We all know how to create an entity bean and access bean for a table that we have added newly if it contains String, Integer, Long etc data types. But when our new table has Oracle CLOB as the data type for one of the column, our access bean will not work properly. In this case in order to make our access bean working with CLOB/BLOB data type we need to edit the entity bean methods and use a session bean to store and retrieve the CLOB data. Follow the below steps to access an Oracle CLOB field from a CMP Entity bean. The trick is to use the JDBC calls to store and retrieve the CLOB data.

I am assuming that you have created a new entity bean (say NewTableBean) that has a field named ‘value’ which is of type Oracle CLOB. The data type for value field in our entity bean is a String.

1. The first step in making CLOB working for an entity bean is to create a session bean to perform our JDBC calls. When creating the session bean make sure that you have extended the session bean from com.ibm.commerce.base.helpers.BaseJDBCHelper

2. Add a method for retrieving the CLOB data using JDBC calls. The code for reading CLOB using JDBC is given below. In this case I named the method as findValueByPrimaryKey.

	public String findValueByPrimaryKey(Long primaryKey) 
			throws NamingException, SQLException {
		makeConnection();
 
		PreparedStatement ps = getPreparedStatement(
				"SELECT VALUE FROM XNEWTABLE WHERE NEWTABLE_ID = ?");
		ps.setLong(1, primaryKey.longValue());
 
		String stringTemp = null;
 
		try {
			ResultSet rs = executeQuery(ps, false);
 
			if (rs.next()) {
				Clob clobTemp = rs.getClob(1);
 
				if ((clobTemp == null) || ((int) clobTemp.length() == 0)) {
					stringTemp = null;
				} else {
					stringTemp = clobTemp.getSubString(1,
							(int) clobTemp.length());
				}
			}
		} finally {
			closeConnection();
		}
 
		return stringTemp;
	}

3. Add a method for updating the CLOB data using JDBC calls. The code for updating CLOB using JDBC is given below. In this case I have named my method as updateValueByPrimaryKey

	 public int updateValueByPrimaryKey(Long primaryKey,
		String value) throws NamingException, SQLException {
		int rowCount = -1;
		makeConnection();
 
		PreparedStatement stmt = getPreparedStatement(
				"UPDATE SET VALUE =? WHERE NEWTABLE_ID  =?");
		stmt.setLong(2, primaryKey.longValue());
 
		if (value == null) {
			stmt.setNull(1, Types.CLOB);
			rowCount = executeUpdate(stmt, false);
		} else {
			try {
				PreparedStatement stmt1 = getPreparedStatement(
						"UPDATE XNEWTABLE SET VALUE = empty_clob()  WHERE NEWTABLE_ID = ?");
				stmt1.setLong(1, primaryKey.longValue());
				stmt1.executeUpdate();
 
				PreparedStatement stmt2 = getPreparedStatement(
						"SELECT VALUE FROM XNEWTABLE WHERE NEWTABLE_ID=? FOR UPDATE");
				stmt2.setLong(1, primaryKey.longValue());
 
				ResultSet rs = stmt2.executeQuery();
 
				if (rs.next()) {
					Clob myClob = rs.getClob("VALUE");
					Writer writer = null;
					writer = ((oracle.sql.CLOB) myClob).getCharacterOutputStream();
 
					char[] aXMLDataData = value.toCharArray();
					writer.write(aXMLDataData);
					writer.flush();
					writer.close();
				} else {
					throw new ObjectNotFoundException();
				}
 
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
 
			rowCount = 1;
		}
 
		return rowCount;
	}

Now we have both the methods for updating and reading the CLOB column is ready. The next step is to call these functions from our entity bean so that the CLOB columns also will be updated/retrieved when we use our access bean.

4. Open the entity bean class we have created and search for a method called _copyFromEJB. Replace the following code

	h.put("value", getType());

with this code.

	NewTableHelperSessionBean sbNewTable = new NewTableHelperSessionBean();
 
	try {
		h.put("value",
			sbNewTable.findValueByPrimaryKey(
				newTableId));
	} catch (NamingException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}

Here NewTableHelperSessionBean is the session bean we have created for performing our CLOB JDBC operations.

The above code will make sure that our session bean code will be called when we access the CLOB field.

5. The next step is to call the session bean update method whenever we update our entity bean. For that open the entity bean class we have created and search for a method called _copyToEJB. Replace the following code

	 if (h.containsKey("value")) {
		setValue((localValue));
	}

With this one.

	NewTableHelperSessionBean sbNewTable = new NewTableHelperSessionBean();
 
	try {
		if (h.containsKey("value")) {
			sbNewTable.updateValueByPrimaryKey(newTableId,localValue);
		}
	} catch (NamingException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}

This will make sure that whenever the user calls the commitCopyHeler our CLOB field will be properly updated by calling the new session bean method.

We are done with all the required changes to make our entity bean working even for a CLOB column type. Now generate the deployment and RMIC code and start using your access bean/ entity bean. Have fun.

  • Share/Bookmark

Read More

Post a Comment