Wednesday, November 11, 2009

Narrow the lookup...EJB

With Java EE 5 we can simply cast the EJB look-up to one of the remote interfaces.
But this will not do with J2EE.

We should use javax.rmi.PortableObject.narrow method to narrow the EJB look-up to home interface.

And the reason is, J2EE implementation requires RMI-IIOP to implement the remote interfaces.This RMI-IIOP demands that the real object must be wrapped around with a portable object.

This portable object encapsulates information about the real remote object.The client has to interogate the portable object to find the real remote object.This process ( of obtaining the real object from the portable object) is called "narrowing".

Monday, November 2, 2009

@TableGenerator - ID for your entities

@TableGenerator annotation lets you define auto generated id columns for your entities.
In a recent project with MySQL as the backend , I couldn’t create a sequence and tie it up with the JPA entity ( Still wondering how to do that…)
To resolve this, I made use of @TableGenerator annotation , created a table to store the latest id and tied it up with the entity.

@Id
@Column(name="STUDENT_ID")
@TableGenerator(name="studentkey",
table="tbl_keys",
valueColumnName="std_key",
pkColumnName="std_key_name",
pkColumnValue="studentkey",
allocationSize=1)
@GeneratedValue(strategy=GenerationType.TABLE,
generator="studentkey")
private long studentId;


The @TableGenerator annotation defines that “TBL_KEYS” is the table to store the generated id for the entity.
STD_KEY is the column to store the generated id
STD_KEY_NAME is the column which has the key-name entries. The key name here is “studentkey” .
You can add another entry in TBL_KEYS named “mark_key” and map it to another entity TBL_MARKS.
@GeneratedValue annotation specifies that the id generated is of GenerationType.TABLE type and maps the already defined @TableGenerator with given name.