Thursday, January 19, 2006

Using DateFormat/ Calendar

Using DateFormat:

import java.text.DateFormat;
Date date = DateFormat.getDateInstance(DateFormat.SHORT).parse("19/01/2006");
System.out.println(date);

Using calendar:

import java.util.Calendar;
new Date(Calendar.getInstance().getTimeInMillis())

simple...

Convert from java.util.Date to java.sql.Date

use the code like this:

java.util.date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println(sqlDate);

simple...

Wednesday, January 18, 2006

How To use a finder method returning more than one row in an ejb-ql?

1) write the ejb -ql in the EJB Deployment Descriptor as like:

method: findByObjectArray
ejb-ql: select object(o) from table_nm o where o.seq_id_fk = ?1

2) in the CMP Local interface, declare the finder fn:

public java.util.Collection findByObjectArray(java.lang.Integer SeqId) throws javax.ejb.FinderException;

Using the finder method:

3) call the finder method on local home. Initialise the iterator and use this iterator to access all the rows returned.

Collection newCollect = ejbLocalHm.findByObjectArray(seq_id);
Iterator iter = newCollect.iterator();

pretty simple for those who know...

Tuesday, January 17, 2006

Getting Primary Key Value (Custom Finder Method)

I created a session facade for my entity beans. In my entity beans apart from the findByPrimaryKey method I had a another finder method, based on another two attributes of the entity bean. Now the problem was how to get the value of the primary key, when i ran this finder method.

The solution:
Key seqKey = () ejbLocal.getPrimaryKey);
use: seqKey.

pretty simple for those who know...