JSP 2.1 features – trim whitespace directive

Put this in you JSP and it will remove the whitespace for you:

<%@page trimDirectiveWhitespaces="true"%>

Read more about it here: http://java.sun.com/developer/technicalArticles/J2EE/jsp_21/

XML Serialization in Java – when you have a CLOB data type in DB

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class MyBean
  private String name;

  public void setName( String name )
    this.name = name;

  public String getName()
    return name;

  public static void main( String[] args )
    MyBean b=new MyBean();
    b.setName( "Hello World" );
    ByteArrayOutputStream out=new ByteArrayOutputStream( 200 );
    XMLEncoder enc=new XMLEncoder( out );
    enc.writeObject( b );
    ByteArrayInputStream in=new ByteArrayInputStream( out.toString().getBytes() );
    XMLDecoder dec=new XMLDecoder( in );
    MyBean b2=(MyBean)dec.readObject();

JPA or hibernate annotations – one to one mapping using shared primary key

The example given in hibernate documention for a shared primary key doesn’t work while inserting records. The other end of the one to one relationship gets inserted with a primary key of zero (if the id is primitive) or the insertion will totally fail if the id is reference type. Use this solution for one to one shared primary key:  http://stackoverflow.com/questions/787698/jpa-hibernate-one-to-one-relationship

Note that for this approach to work you’ll have to use a bidirectional relationship. (No harm in doing so, just involves a little extra coding). On both ends (lets call them A and B), you’ll have the @OneToOne. On one end “B” we will have a mappedBy attribute mentioning the name of the property on the other end “A” where the mapping is mentioned (similar to inverse=”true”). The same “B” will also have a generator on ‘id’ that will use the ‘id’ on property pointing to end “A”. One end “A” you can use a sequence to generate the ‘id’ and the same value will be inserted as the ‘id’ for B also (hence the name ‘shared key’). Here is the code:

public class MemberComment

@SequenceGenerator(name=”MEMBERCOMMENTID_SEQ”, sequenceName=”MEMBERCOMMENTID_SEQ”,allocationSize=1)
long memberCommentId;

@PrimaryKeyJoinColumn //Hibernate will know that it will have to look for a record with the same primaryKey in the other end while retrieving by looking at this annotation
private MemberCommentCode memberCommentCode;



public class MemberCommentCode

@GeneratedValue( generator = “customGen” )
@GenericGenerator( name = “customGen”, strategy = “foreign”, parameters = @Parameter( name = “property”, value = “memberComment” ) ) //For using the id from memberComment property- this property needs to be set before saving this record.
long memberCommentCodeId;

@OneToOne(mappedBy=”memberCommentCode”)//tells hibernate to ignore saving this record while saving the ‘MemberCommentCode’ record, thus preventing double insertions – one while inserting MemberComment another while inserting MemberCommentCode
@PrimaryKeyJoinColumn//tells hibernate to use primaryKeys for joining during retrieval
private MemberComment memberComment;